无法将类型 int' 隐式转换为 unityengine.vector3'

cannot implicitly convert type int' to unityengine.vector3'

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

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'

transform.position 是 Vector3 类型。你应该改为 pos.x > 100 而不是 pos > 100 inside the if condition.

这里你犯了两个错误。

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

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

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

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