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

标签 c# unity-game-engine

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

"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; 使用 System.Collections;

公共(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/

相关文章:

c# - 如何在 MVC Core 和 AutoFac 中使用属性注入(inject)

c# - 如何在C#中获取Windows驱动器的点击事件?

unity-game-engine - 如何从 URL 统一加载 3D 模型?

unity-game-engine - unity hub 不允许我安装 unity;错误: "There is not enough space to download and install the selected items"

android - 直接显示排行榜分数 - Google Play 服务

android - Unity 对应用程序 Split 的支持

c# - 在 dot net core 中初始化后台服务的正确方法

c# - 除了静态构造函数执行时间之外,beforefieldinit 可以有任何影响吗?

c# - 使用 Serilog 时如何覆盖 appsettings.json 中指定的应用程序名称?

c# - Unity 2D项目结构: How to create Player dynamically based on script