- Home /
 
C# move y position of object not working
Hi im having a problem getting this script to properly reset the y position of the object after the condition is met. When the condition is met it only changes the value but not the actual y position of the object. So im wondering why is this happening? Thank You.
 public GameObject sceneManager;
 public Transform collisionExplosion;
 
 public Vector3 position;
 public float posY;
     
     void Awake ()
     {
         posY = transform.position.y;
         
     }
     // Update is called once per frame
     void Update () 
     {
         posY = transform.position.y;
         
 
         move();
         reset();
         
     }
 
     void move ()
     {
         float shipSpeed = 7.0f;
         float totalSpeed = shipSpeed * Time.deltaTime;
         transform.Translate(Vector3.down * totalSpeed);
     }
 
     void reset ()
     {
              
 
         if(posY <= -6)
         {
             position = new Vector3(Random.Range (-6.5f,6.5f),8,0);
             
         }
 
     } 
 
     void OnTriggerEnter(Collider other)
     {
                 
 
         if(other.gameObject.tag == "Player")
         {
             
     
         //Create explosion on ship collision
             Instantiate(collisionExplosion, other.transform.position, other.transform.rotation);
     
     
         //Reset the position of the enemy
             position = new Vector3(Random.Range (-6.5f,6.5f),8,0);
         }
     }
 
     
 
              Answer by Maulik2208 · Jan 07, 2013 at 07:07 AM
 public GameObject sceneManager;
 public Transform collisionExplosion;
 
 public Vector3 position;
 public float posY;
 
     void Awake ()
     {
        posY = transform.position.y;
 
     }
     // Update is called once per frame
     void Update () 
     {
       // posY = transform.position.y;
       // Update is called after every frame so posY is getting transform.position.y after each frame so it's better to use this in start function if you want to pass the y position just once.....
 
 
        move();
        reset();
 
     }
 
     void move ()
     {
        float shipSpeed = 7.0f;
        float totalSpeed = shipSpeed * Time.deltaTime;
        transform.Translate(Vector3.down * totalSpeed);
     }
 
     void reset ()
     {
 
 
        if(posY <= -6)
        {
          transform.position /*position*/ = new Vector3(Random.Range (-6.5f,6.5f),8,0);
 
        }
 
     } 
 
     void OnTriggerEnter(Collider other)
     {
 
 
        if(other.gameObject.tag == "Player")
        {
 
 
        //Create explosion on ship collision
          Instantiate(collisionExplosion, other.transform.position, other.transform.rotation);
 
 
        //Reset the position of the enemy
         transform.position/* position*/ = new Vector3(Random.Range (-6.5f,6.5f),8,0);
        }
     }
 
              Tried to fix the issue have a look at above script....if Still your problem not solved then please provide some more info regarding script.....Enjoy and Don't forget to mark the answer if found useful
Answer by ConsciousCoder · Jan 07, 2013 at 08:15 AM
Thank you so much for your quick response. Appreciate it.
Glad that you found it useful......Cheers...Enjoy.....here all the people are trying to solve the problems as soon as possible....So, have a Happy Coding........
Your answer