【问题标题】:A using namespace directive can only be applied to namespaces, for using UnityEngine. Transform;using 命名空间指令只能应用于命名空间,以便使用 UnityEngine。转换;
【发布时间】:2022-10-14 00:54:44
【问题描述】:

// hello i get this error when i run this code: Assets\Scripts\enemy.cs(4,7): error CS0138: A 'using namespace' directive can only be applied to namespaces; 'Transform' is a type not a namespace. Consider a 'using static' directive instead

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.Transform;
                             
public class enemy : MonoBehaviour
{
    public float speed;
    public Transform [] waypoints;
    public Transform [] target;
    private int destPoint=0;
    // Start is called before the first frame update
    void Start()
    {
        target = waypoints[0];
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 dir=target.position - Transform.position;
        Transform . Translate(dir.normalized * speed* Time.deltaTime, Space . World);

        if(Vector3.Distance(Transform.position, target.position) <0.3f)
        {
            destPoint = (destPoint + 1)   % waypoints.Length ; 
            target = waypoints[destPoint];     
             }
        
    }
}

// Thanks

//if I delete using UnityEngine.Transform; there will be a lot of errors :

.cs(18,18):错误 CS0029:无法将类型“UnityEngine.Transform”隐式转换为“UnityEngine.Transform[]

.cs(24,28):错误 CS1061:“Transform[]”不包含“position”的定义,并且找不到接受“Transform[]”类型的第一个参数的可访问扩展方法“position”(你是缺少 using 指令或程序集引用?)

cs(24,39):错误 CS0120:非静态字段、方法或属性“Transform.position”需要对象引用

.cs(25,9):错误 CS0120:非静态字段、方法或属性“Transform.Translate(Vector3, Space)”需要对象引用

cs(27,29):错误 CS0120:非静态字段、方法或属性“Transform.position”需要对象引用

cs(27,56): 错误 CS1061: 'Transform[]' 不包含'position' 的定义,并且找不到接受'Transform[]' 类型的第一个参数的可访问扩展方法'position' (你错过了吗? using 指令或程序集引用?)

cs(30,22):错误 CS0029:无法将类型“UnityEngine.Transform”隐式转换为“UnityEngine.Transform[]”

【问题讨论】:

  • 删除这一行:using UnityEngine.Transform; 你不需要它。看起来您还有其他一些错误,您也尝试将类型 Transform 用作变量。
  • target = waypoints[destPoint]; 是您的问题:targetTransform 的数组,waypoints[destPoint]Transform,您不能将一个分配给另一个。你想做什么?

标签: c# unity3d


【解决方案1】:

这是因为 Transform 是一个类而不是 UnityEngine 命名空间内的命名空间,所以你应该删除 using UnityEngine.Transform;并保持其他一切不变。

【讨论】:

  • 是的,但是有很多错误之后的问题。
  • 之后它给出了什么错误?
  • AssetsScriptsennemy.cs(18,18):错误 CS0029:无法将类型“UnityEngine.Transform”隐式转换为“UnityEngine.Transform[]”。或 .cs(24,28):错误 CS1061:“Transform[]”不包含“位置”的定义,并且找不到接受“Transform []”类型的第一个参数的可访问扩展方法“位置”(是您缺少 using 指令或程序集引用?)或 cs(24,39):错误 CS0120:非静态字段、方法或属性“Transform.position”等需要对象引用
  • Transform.position 是不可能的,你必须使用 targetObject.transform.position
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 2011-02-26
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
相关资源
最近更新 更多