无法将类型对象隐式转换为unityengine.矢量3

本文关键字:转换 unityengine 矢量 类型 对象 | 更新日期: 2023-09-27 18:20:52

我想在场景中放置六个对象(球)。我认为代码看起来可行,但我收到了一条控制台消息。信息:

"Assets/GameScripts/Instance.cs(26,40):错误CS0266:无法隐式转换类型object' to UnityEngine.Vector3'。显式转换存在(您是否缺少一个演员阵容?)"

使用UnityEngine;使用System.Collections;

公共类实例:MonoBehavior{公共GameObject球;

public ArrayList coordinateContainer = new ArrayList();

// Use this for initialization
void Start () {
    coordinateContainer.Add(new Vector3(1f,1f,1f));
    coordinateContainer.Add(new Vector3(2f,1f,1f));
    coordinateContainer.Add(new Vector3(3f,1f,1f));
    coordinateContainer.Add(new Vector3(4f,1f,1f));
    coordinateContainer.Add(new Vector3(5f,1f,1f));
    coordinateContainer.Add(new Vector3(6f,1f,1f));

    //ball.transform.position = new Vector3(1f,1f,1f);
    ball.transform.rotation = Quaternion.identity;
    for (int i = 0; i <  6; i++) {
        ball.transform.position = coordinateContainer[i];
        Instantiate(ball,ball.transform.position,ball.transform.rotation);
    }
}
// Update is called once per frame
void Update () {
}

}

无法将类型对象隐式转换为unityengine.矢量3

由于您使用的是ArrayList,因此矢量存储为objects。试试这个

ball.transform.position = (Vector3)coordinateContainer[i];

最好使用List<Vector3>而不是ArrayList,这样可以避免强制转换(因为List<T>只能容纳T类型的对象)。