【发布时间】: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];是您的问题:target是Transform的数组,waypoints[destPoint]是Transform,您不能将一个分配给另一个。你想做什么?