2

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

“Assets/GameScripts/Instance.cs(26,40):错误 CS0266:无法隐式转换类型object' toUnityEngine.Vector3'。存在显式转换(您是否缺少演员表?)”

使用 UnityEngine;使用 System.Collections;

公共类实例:MonoBehaviour {公共游戏对象球;

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 () {

}

}

4

1 回答 1

5

由于您使用的ArrayList是向量,因此向量被存储为objects. 尝试这个

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

你可能会更好,List<Vector3>而不是 ArrayList 这样你就可以避免强制转换(因为List<T>只能保存 type 的对象T)。

于 2013-07-03T08:16:11.583 回答