GameObject.transform.position not working

when i try to specify a maxHeight and minHeight the object Disappear.

using UnityEngine; using System.Collections;

public class AviaoControl : MonoBehaviour {

 public GameObject aviao;
 public float maxHeight;
 public float minHeight;
 public float positionX;
 public float speed;
 void Start () {
 
 }
 void Update () {
     float translation = Input.GetAxis ("Vertical") * speed;
     aviao.transform.Translate(0,translation,0);
     if (aviao.transform.position.y > maxHeight) {
         aviao.transform.position = new Vector2(0,maxHeight);
                    //Why the object disappear when enter here?
     }
     if (aviao.transform.position.y < minHeight) {
         aviao.transform.position = new Vector2(0,minHeight);
                    //Why the object disappear when enter here?
     }
 }
}

the object move normally but, when it enter in if the object disappear in the game. The values max nad MinHeight are correct. I’m using unity 5.2

It is hard to believe that the object is really disappearing (= destroyed). Check what happens to it and where it’s position is in the Inspector, when it is “disappearing”.

You gave too few information to nail the problem down right away.

  • Is AviaoControl.cs the only script that changes the position of aviao?
  • What happens when you just use a clamp function to restrict the height? Mathf.Clamp ?
  • Translate() is translating in local space by default, and you are checking the world position. if you want it to be in World Space you have to set the 2nd parameter to Space.World. => Transform.Translate()

Have a nice day.