unity stops responding when ever i use if statement in loop

unity stops responding when ever I use if statement in the loop.

int i = 0;
        while (i < Index)
        {
            transform.position = Vector3.MoveTowards(transform.position, way.Points*.transform.position, step);*

}
if (transform.position == way.Points[Index].transform.position)
{
i++;
}

There’s a few issues in the code.

The first one is, as everyone pointed out, that i cannot become equal to or greater than Index while the loop is running since you don’t increment i anywhere inside the loop. Unity hangs because no other line of code can be executed before you exit the loop. Not even the code that draws Unity’s own UI. Move the if inside the loop

The next problem is that you move transform.position towards way.Points_.transform.position, but the condition in the if clause is to increment i when the position reaches way.Points[Index].transform.position. You are moving it towards one position and expecting it to reach another position. I think you want to use way.Points* in the if condition too._
The third issue is that moving objects gradually in a tight while loop is pointless. The screen doesn’t get redrawn between iterations of the loop (basically the same reason an endless loop caused Unity to hang).
I can’t tell you exactly how to fix this because you don’t tell us what you wanted to do, but just removing the while loop and running this in Update() should work better than what you have. That will move the object a tiny bit every Update() and lets the code execution continue so that you can actually see the changes drawn on the screen.
_
int Index;_
_
int i;_
_
_
_
void Update () {_
if (i < Index) {
_ transform.position = Vector3.MoveTowards(transform.position, way.Points.transform.position, step);
if (transform.position == way.Points.transform.position) {
i++;
}
}
}
A fourth thing (which isn’t necessarily causing any problems in this case) is that since transform.position is a property over which you don’t have total control, and since it is a Vector3 which consists of 3 floating point numbers, comparing equality between 2 transform.positions has a tiny chance of failing when it seemingly should succeed under some rare(ish) conditions.
Things like rotations of possible parent objects etc. can cause tiny floating point precision errors in the position.
Generally it’s safer to establish a “safety margin” and do things when the objects are close enough instead of expecting them to be at the exact same spot.
if ((transform.position - way.Points.transform.position).magnitude < 0.001f) {
transform.position = way.Points.transform.position*

* i++;
}*_

Your if-statement is not inside your loop, and that why i is not incremented and that causes your problem.

The problem is that you never exit the while because i++; is not inside the loop so the condition never change and is every times true so unity is blocked here and can’t make anything other.

Edit : If you don’t want that Unity crash when you try to use while with a bad way, is to use IEnumerator, it separate this in a separate thread so unity can continue to work.