- Home /
Everything freezes on " yield return new WaitForSeconds(); " !!!
Ok! all of my code in this scene is in one script and one manager object. all of it is about 700 lines. so I can't put it here. I tested different things:
1) switch platform from android to pc/mac
2) test on a previous version of unity( previous 2017, and current on is 2018.1 )
none of them solve the problem. then I change some part of the code that I suspected to cause the problem. ( none of them solve the solution ). then I started to put Debug.Log()s everywhere. so I found where it freezes.
Here Is the code:
IEnumerator ShowSigns(int Button1State, int EqualState, int Button2State)
{
Debug.Log("ShowSigns");
if (Button1State == 1)
{
OperationOneCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
}
else if (Button1State == 2)
{
OperationOneIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
}
if (EqualState == 1)
{
EqualCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
}
else if (EqualState == 2)
{
EqualIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
}
if (Button2State == 1)
{
OperationTwoCorrectSign.GetComponent<CanvasGroup>().alpha = 1;
}
else if (Button2State == 2)
{
OperationTwoIncorrectSign.GetComponent<CanvasGroup>().alpha = 1;
}
Debug.Log("BeforeWaiting");
yield return new WaitForSeconds(0.3f);
Debug.Log("AfterWaiting");
OperationOneCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
OperationOneIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
EqualCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
EqualIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
OperationTwoCorrectSign.GetComponent<CanvasGroup>().alpha = 0;
OperationTwoIncorrectSign.GetComponent<CanvasGroup>().alpha = 0;
state = GameState.CreateNewProblem;
Debug.Log("EndSigns");
}
I found that it freezes on this:
yield return new WaitForSeconds(0.3f);
Very strange!!!
This is a picture of the game.
The game is a simple game that shows 2 math phrase and player should choose the bigger or equal. The logic is this way:
1) make new phrases and change the game state to "ChooseAnswer"
2) player press one of 3 buttons and the answer checked and score and other things changes and the ShowSigns coroutine will start and ends after 0.3 seconds. and as you see at the end of the coroutine state changes to "CreateNewProblem".
3) in the Update when CreateNewProblem detects, the code call for the NewProblem() function to make new phrases and at the end of that game state changes to "ChooseAnswer".
this logic repeats over and over until time reaches zero. a "step" variable increase and decrease by 1 by any correct and incorrect answer. and a variable level = steps/10 determines the difficulty of phrases.
the game works correctly on %98 click On buttons. but usually, it freezes somewhere after step 20. In 21, 23, 27, 34 ... very randomly. but always after 20 and some time no freeze until time ends. and always right before yield return. exactly at the same line.
I read many questions and answers but none of them was helpful. I have no while loop, no while(true), as long as I know and check my code no infinite loop, on StopAllCoroutines ... nothing. and I stuck for 2 days. thanks all of you for helping. Oh, and here is the code file. I just change the format to .txt
You can put code in https://hastebin.com/ or https://gist.github.com/
You can also report bug to Unity https://unity3d.com/unity/qa/bug-reporting
Your code is pure Spaghetti code. It's really hard to actually follow the control flow. You really should split up your code into logical sections and use more descriptive variables and method names. Also you seem to use the wrong Random.Range limits in almost all cases. When you use the integer version of Random.Range the upper limit is exclusive. So Random.Range(1,2)
will always return "1". Random.Range(1,9)
will return a value between 1 and 8 inclusive but never 9
Answer by Armamedia · May 31, 2018 at 01:53 PM
OK, I tried some math equations in different difficulty types instead of those while loops. Check the code file that I attached. In GeneratePhrase() method. The actual Problem was there. And now it works perfectly. No freezing anymore. And even I get better Phrases now. and I still use that while loop inside CountDown coroutine and have no problem. ( using while loops is Ok. in another forum someone said "never use while loops in unity. while loops in unity always cause problems." wrong! ) take a look at this link for more info
And probably the problem was using a RandomRange to tell the code that when it must exit the while loop. I suspected that Random.Range() could be the problem but it usually stayed in while loop less than 4 times looping. so I ignore that it can be stuck there in case of a very bad luck.
unfortunately, in coding, the bad luck always occurs.
Answer by Legend_Bacon · May 30, 2018 at 03:54 PM
Hello there,
After checking your code out a little bit, I don't see anything that would cause this issue. My suggestions would be:
• When "it freezes", do you mean the Unity Editor or the game? Can you still use Unity or do you have to go to the task manager and manually terminate it?
• If it's just the game that freezes, then my guess would be that somehow your timescale gets set to 0 somewhere. That means the WaitForSeconds() never ends. Could you try replacing it with WaitForSecondsRealtime() and see if it helps?
• Also, kudos on actually using coroutine assigning to start and stop them properly. Not a lot of people do that.
I hope that helps!
Cheers,
~LegendBacon
thank you.
first, the game and the editor both freezes and I have to go to the task manager and manually ter$$anonymous$$ate it. yes.
second I never change timescale in my code. and is it possible that it changed to 0 by itself, somehow?!
third that yes I stop the ShowSigns() coroutine every time after starting and the only coroutine that acts continuously is the CountDown().
the only solution that I found was to remove while loops and don't use them and strangely solves the problem. he sais using whiles in unity always cause problems that mostly are untrackable. but coding without while loops could be very painful in some cases. I'm looking for a better solution. at least a good alternative to while loops.
Answer by jim3878 · May 30, 2018 at 04:37 PM
mm....you start ShowSign by operation1Button, operation2Button, and equalButton ,but only set state to GameState.CheckingAnswer(line 129) in operation1Button.
i think you should change to
operation1Button.onClick.AddListener(() =>
{
Debug.Log("ChooseAnswer");
if ( state == GameState.ChooseAnswer)
{
state = GameState.CheckingAnswer;
//....
}
});
equalButton.onClick.AddListener(() =>
{
Debug.Log("ChooseAnswer");
if (state == GameState.ChooseAnswer)
{
state = GameState.CheckingAnswer;
//....
}
});
operation2Button.onClick.AddListener(() =>
{
Debug.Log("ChooseAnswer");
if (state == GameState.ChooseAnswer)
{
state = GameState.CheckingAnswer;
//....
}
});
hope that helps
thank you for that close looking at my code. but I change it, correct it. but still, have problem.
i guest problem is on your while loop
while (result < 1 || result > 50 || result < (recommendedNumber - 2) || result > (recommendedNumber + 2))
{
numberOne = UnityEngine.Random.Range(1, 9);
numberTwo = UnityEngine.Random.Range(1, 9);
result = numberOne + numberTwo;
}
or
while (result < 1 || result > 50 || result < (recommendedNumber - 2) || result > (recommendedNumber + 2))
{
numberOne = UnityEngine.Random.Range(1, 9);
numberTwo = UnityEngine.Random.Range(1, 9);
result = numberOne * numberTwo;
}
I suggest adding a counter to break it like this
int count=0;
while (count<100&&( result < 1 || result > 50 || result < (recommendedNumber - 2) || result > (recommendedNumber + 2)))
{
count++;
numberOne = UnityEngine.Random.Range(1, 9);
numberTwo = UnityEngine.Random.Range(1, 9);
result = numberOne * numberTwo;
}
if(count==100){
throw new Exception("while is broken.");
}
i am not sure if it is Infinite Loop because i dont know what your recommendedNumber mean
Your answer
Follow this Question
Related Questions
Weapon Movement Freezing 0 Answers
Coroutine crashes game 2 Answers
Unity (2018.2f and 5.6) Freezes when using While Loop for countdown timer in coroutine. 3 Answers
Freeze when building 0 Answers
Coroutine making unity freeze 2 Answers