- Home /
Why does Unity crash with this code ?
I was trying to implement an idle walk for my npc. The implementation is quite rough since I am a beginner and i know that there is probably a better way but this was the only way i could think of.
Here´s the code :
public class Npc : MonoBehaviour
{
// Initializing the timer and the time it takes for the NPC to choose where to go in this case 5 seconds.
float timer = 0.0f;
float timerMax = 5.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime; // Counting to 5
Vector3 stepNegative = new Vector3(-0.5f, 0f, 0f); // Steps in either negative x or positive x so the movement looks better and the npc doesn´t teleport
Vector3 stepPostive = new Vector3(0.5f, 0f, 0f);
Vector3 randomDirection = new Vector3(Random.Range(-10f, 10f), 0f, 0f); // Choosing a spot on x Axis for the NPC to go
if (timer >= timerMax)
{
if(randomDirection.x > transform.position.x) // Checking if the "randomDirection" is to the left or the right and taking either negative or positive steps accordingly
{
while(randomDirection.x != transform.position.x * Time.deltaTime)
{
transform.position += stepPostive * Time.deltaTime;
}
}
else
{
while (randomDirection.x != transform.position.x)
{
transform.position += stepNegative * Time.deltaTime;
}
}
// Note: " * Time.deltaTime " was added because i saw it in another movement script but i dont know what it does or if it is usefull in this situation
}
}
}
Answer by tuinal · Jul 28, 2020 at 09:16 PM
NB Unity does not crash per se, it's just in an infinite loop of your own design. Update needs to complete for the next frame to happen; you're attempting to 'move' something incrementally in update (i.e. within a single frame), when you need to instead think in terms of these individual frames happening (i.e. move it once towards the target by x distance).
A 'while' loop in update is usually a mistake, as there are very few use cases where you'd deliberately want to iterate a process until a condition within a single frame, because conditions within a single frame (i.e. positions, user input, etc.,) are static.
You can recover it by opening your IDE (e.g. Visual Studio) and stopping the execution there.
At first I didn´t quite understand what you meant by "in terms of individual frames happening" not because it wasn´t explained very well but because I am not very smart. Now, after reading it a few times, I realized what I did wrong and basically everything works now.
Thank you very much. Your well explained answer helped a lot.
Answer by deniskotpletnev · Jul 28, 2020 at 08:15 PM
I don't quite understand why the while loop is needed here:
while (randomDirection.x != transform.position.x * Time.deltaTime)
{
transform.position += stepNegative * Time.deltaTime;
}
and here
while (randomDirection.x != transform.position.x)
{
transform.position += stepNegative * Time.deltaTime;
}
Try to remove it, most likely an infinite loop will form, because the condition of the while loop is never met. If so, then just change the conditions