- Home /
Unity freezes after running this basic code.
private void OpenDoorAnimation()
{
float startTime = Time.time;
float timeDiff;
int i = 0;
while (i < 23)
{
timeDiff = Math.Abs(Time.time - startTime);
if (timeDiff > 5.0f)
{
i++;
startTime = Time.time;
}
}
}
Unity always freezes after the code reaches the while loop. Been staring at this for hours, Im either brain dead or going insane. Send help.
Basically, a button is pressed, and another method is called, that then calls this method. Absolutely nothing else happens in the code. I know the button works because ive tested it with multiple Debug.Log methods. But it can never reach the Debug.Log message that I would put inside the loop. Unity freezes before that happens. I always have to force close unity with task manager and restart it.
Your probably wondering why the method is called 'OpenDoorAnimation'. Well inside the if-statement, its suppose to switch to the next sprite every 5 seconds, from sprites in an array. And you guessed it, there is 24 different sprites. Hence the i < 23. But that doesnt really matter, cause Unity wants to freeze every time I run this simple loop.
Answer by Bunny83 · Oct 19, 2020 at 10:11 AM
Of course it will freeze. Though not after running your code but while your running your code because you have an infinite loop. Lets break it down:
Your while loop will iterate as long as i is smaller than 23. Inside your while loop you only ever increase i inside your if statement. That means when the if statement is false you will not enter the if statement and you're looping in the while loop for ever.
So the next question is when is your if statement actually true? The answer is also quite simple: never. That's because you trapped Unity inside your while loop. Time.time gives you the time at the start of the current frame. It does not change during the execution of the current frame. Since you are stuck in your while loop without ever returning the control back to Unity, Unity will never complete the current frame and can never update the time property.
Even if you used an asynchronous time source (like System.DateTime.Now) Unity would still be frozen during that "wait" time.
You may want to use a coroutine for this. Though you don't seem to currently use your "i" value anywhere. So it's difficult to give an example since your loop currently is a "useless" loop.
I would probably use something like this:
private void OpenDoorAnimation()
{
StartCoroutine(OpenDoor());
}
private IEnumerator OpenDoor()
{
for(int i = 0; i < spriteArray.Length; i++)
{
// use i here to change your sprite
yield return new WaitForSeconds(5f);
}
}
Answer by mgw2021 · Oct 08, 2021 at 02:35 AM
I try to run the testing and it block the UI updating. I expect text must be updated then only execute the while looping, but it is not. i found out that once the following scripts completed and unity only run UI updating for "this.Button.GetComponentInChildren().gameObject.SetActive(false);" and "Debug.Log". any idea how to resolve this issue?
public void Click() {
this.Button.GetComponentInChildren<Text>().gameObject.SetActive(false);
float time = 0;
while (time <= 1000000)
{
time++;
Debug.Log("Im doing heavy work");
}
}
Your answer
Follow this Question
Related Questions
How to resize a player every couple of seconds? 2 Answers
TimeSpan does the difference automatically count the minutes 1 Answer
Problem With float 2 Answers
if and invoke repeating in start... 1 Answer
Why is this script I made crashing unity 3 Answers