transform.Translate doesn't work when it's in a while loop in Start() function

Hello citizens.
transform.Translate method doesn’t work when it’s in a while loop in Start() function but it works perfectly when it’s in Update() method. I controlled if it exits while loop by Debug.Log but it doesn’t exit till end. So, I really wonder what my problem is.

void Start()
    {
        length = transform.position.y - GameObject.Find("Finish").transform.position.y;
        while (transform.position.y > GameObject.Find("Finish").transform.position.y)
        { 
            Debug.Log(speed2); //prints the values i expected
            speed2 = 1 + transform.position.y - GameObject.Find("Finish").transform.position.y;
            transform.Translate(Vector3.down * speed2/100, Space.World); //doesn't work
        }
        Debug.Log("Donus");
        while (transform.position.y < 0)
        { 
            Debug.Log(speed2); //prints the values i expected
            speed2 = 1 + transform.position.y - GameObject.Find("Finish").transform.position.y;
            transform.Translate(Vector3.up * speed2/100, Space.World); //doesn't work
        }
    }


    void Update()
    {
        transform.Translate(Vector3.down * speed, Space.World); //works perfectly
    }

I’m a bit confused as to why you’re using while loops here and not something like:

Vector3 finishPos = GameObject.Find("Finish").transform.position.y;

if (transform.position.y > finishPos .y)
{
  transform.position = new Vector3(transform.position.x, finishPos .y, transform.position.z);
}

if (transform.position.y < 0f)
{
  transform.position = new Vector3(transform.position.x, 0f, transform.position.z);
}

_
I’m not familiar with the transform.Translate function which is a surprise as I’ve used unity for a few years now! However I reccomend against translating the transform multiple times in a single function.

If you’re trying to have the player gradually move within these bounds on screen it will not work in the start function or in the a while loop. You will need to gradually translate the player in the Update function.

Void Update()

 Vector3 finishPos = GameObject.Find("Finish").transform.position.y; // note - GameObject.Find is inefficient, if you're using the finish position regularly store it at the top of your class to avoid repeating this call.
 
 if (transform.position.y > finishPos .y)
 {
         speed2 = 1 + transform.position.y - finishPos .y;
         Debug.Log(Speed2); // place this after calculating the speed to get the current value!
         transform.Translate(Vector3.up * speed2 * Time.deltaTime, Space.World);
 }

_

The way I usually handle a more complex transform movement (on an object that doesn’t have a character controller) will be to set a Vector3 newPosition or in your case a newTranslation and apply it only once at the end.

_

For gameobjects with character controllers the Move function in the character controller will overwrite your transform adjustment, meaning you’ll need to do all movement via the character controller.

Why are you trying to move something by increments on a while loop in the Start() function? The start function is for the first frame of the game, and a while loop will do everything in it in one frame, so you should just do transform.position = where you want it to go.


Either way, try doing transform.position += Vector3.up * speed2/100.
Also, if speed2 is an int, this might be what’s making it not work. do ((float)speed2)/100.
I recently ran into this same problem.


I hope it works! If it does, please “Accept Answer”!