代码在GameObject上不起作用
public class EnemyMovement : MonoBehaviour
{
public int rotateSpeed = 1;
public int movementSpeed = 1;
private Transform myTransform;
public Transform target;
// Start is called before the first frame update
void Start()
{
GameObject go = GameObject.FindGameObjectsWithTag("Player");
target = go.transform;
myTransform;
}
// Update is called once per frame
void Update()
{
Rotate();
}
void Rotate ()
{
Debug.DrawLine(myTransform.position, target.position, Color.red);
}
}
错误在这里
GameObject go = GameObject.FindGameObjectsWithTag("Player");
target = go.transform;
答案 0 :(得分:5)
GameObject.FindGameObjectsWithTag("Player")
返回类型为GameObject
的对象数组:请参见documentation。由于您的GameObject go
不是数组,而是单个GameObject
,因此您会收到一条错误消息,指出编译器无法将GameObject[]
隐式转换为GameObject
。
如果只需要一个对象,则可能正在寻找GameObject.FindWithTag,其名称如下:
GameObject go = GameObject.FindWithTag("Player");