我不断收到此错误CS0029有人可以帮助我吗?

时间:2019-11-26 20:34:01

标签: c# unity3d

代码在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;

1 个答案:

答案 0 :(得分:5)

GameObject.FindGameObjectsWithTag("Player")返回类型为GameObject的对象数组:请参见documentation。由于您的GameObject go不是数组,而是单个GameObject,因此您会收到一条错误消息,指出编译器无法将GameObject[]隐式转换为GameObject

如果只需要一个对象,则可能正在寻找GameObject.FindWithTag,其名称如下:

GameObject go = GameObject.FindWithTag("Player");