【问题标题】:cannot implicitly convert type int' to unityengine.vector3'无法将类型 int' 隐式转换为 unityengine.vector3'
【发布时间】:2016-08-10 07:27:36
【问题描述】:

嗨,我是新人,所以这是我的代码:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        var pos = transform.position;
        pos.x += 1;
        transform.position = pos;
        string ss = "hellow";

        if(pos>100){
            print (ss);
        }
    }
}

但是在 if 条件行中给我这个错误:无法将 int 类型隐式转换为 unityengine.vector3'

【问题讨论】:

  • 嗯,是的 - 你认为pos > 100 是什么意思? pos是一个位置,一个Vector3...位置大于100是什么意思?
  • try this: if (pos.x > 100) {...}

标签: c# unity3d unity5


【解决方案1】:

transform.position 是 Vector3 类型。您应该改为在 if 条件内执行 pos.x > 100 而不是 pos > 100。

【讨论】:

    【解决方案2】:

    你在这里犯了两个错误。

    首先,在 C# 中,您不能一次修改单个轴的值。您必须重新分配整个向量。

    所以,pos.x += 1; 是错误的,应该是:

    pos = new Vector3(pos.x + 1, pos.y, pos.z);
    

    最后,if(pos>100){ 的测试也是错误的:您应该使用特定轴的值来检查(我认为:if(pos.x>100){)。

    【讨论】:

    • 我只使用 pos.x += 1 并且我的代码工作正常。那你能告诉我什么是vector3或vector1,vector2!?
    • pos.x += 1; 完全没问题
    • 一般来说,不变性有助于推理代码stackoverflow.com/questions/3751911/…
    • 诚然,在您看到使用中的好处之前,它看起来有点学术性
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多