1

hi i am new in unity so this my code :

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);
        }
    }
}

but give me this error in if condition line :cannot implicitly convert type int' to unityengine.vector3'

Erfan
  • 3,059
  • 3
  • 22
  • 49

2 Answers2

2

transform.position is of type Vector3. you should instead do pos.x > 100 instead of pos > 100 inside the if condition.

Smit Davda
  • 638
  • 5
  • 15
2

You made two errors, here.

At first, you cannot modify a value for a single axis at a time, in C#. You have to reassign the entire vector.

So, pos.x += 1; is wrong and it should be:

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

In the end, also the test if(pos>100){ is wrong: you should take a particular axis' value to check (I think: if(pos.x>100){).

Andrea
  • 6,032
  • 2
  • 28
  • 55