gpt4 book ai didi

c# - 无法将类型对象隐式转换为 unityengine.Vector3

转载 作者:行者123 更新时间:2023-11-30 20:56:37 24 4
gpt4 key购买 nike

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

"Assets/GameScripts/Instance.cs(26,40): error CS0266: Cannot implicitly convert type object' toUnityEngine.Vector3'. An explicit conversion exists (are you missing a cast?)"

使用UnityEngine;使用系统集合;

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

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

}

最佳答案

由于您使用的是 ArrayList矢量存储为 objects .试试这个

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

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

关于c# - 无法将类型对象隐式转换为 unityengine.Vector3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17442924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com