Recently I have been trying to track down why my quiz master scripts stop executing after returning from a FadeIn() call to my Fader script. (Full Project Code)
Problem Method
using UnityEngine;
using core;
using System.Collections;
namespace control
{
public class GameManager : MonoBehaviour
{
public IEnumerator StartGame(string subject, int questionsToAsk)
{
fader.FadeOut();
yield return new WaitForSeconds(waitTime);
startScreen.gameObject.SetActive(false);
quizScreen.gameObject.SetActive(true);
scoreKeeper.CheckReferences();
quizControl.LoadQuestions(subject, questionsToAsk);
quizControl.SetNextQuestion();
fader.FadeIn();
yield return new WaitForSeconds(waitTime);
timer.WaitingForAnswer(true);
timer.StartTimer(quizControl.GetAllottedTime());
yield return null;
}
}
}
The execution gets to fader.FadeIn() executes that code but then stops before executing the next command.
I’ve Debug.Logged every single line of code and found no issues. It gets to the end of the Fade in process, even acknowledges that it is at the end, but never executes the next command.
I’ve linked the complete Script Library above and would appreciate any suggestions as I’ve reached a point where literally slamming my head into a wall is more productive.
What are the values set to in the inspector? Based on the code alone, the fade out and fade in will be running together and counteracting each other, because the fade out runs for 3 seconds, but you only wait 1 second after kicking it off. The fade in will start executing before those 3 seconds are up and then you have a coroutine increasing the alpha value, and a coroutine decreasing the alpha value at the same time. Also bare in mind that the FadeIn() takes 3 seconds to complete, but you are only waiting 1 second. The code underneath it may have executed already by the time the fade is complete.
Try this; instead of waiting 1 second before moving on, why not wait for the fade to complete. You can change the FadeToBlack and FadeToTransparent in Fader to be internal and then call them from the StartGame coroutine
No joy, the program still encounters the same bug even calling the FadeToTransparent() directly. I will admit though that it never occurred to me to try cutting the apparent problem method from the loop.
Ironically, the AskNextQuestion(), which does essentially the same thing but without the initial set-up steps works as intended.
I’ve set the waitTime value = to the fade time, everything else is at default still.
There’s a whole lot of missing things here. Missing prefabs, missing packages. I managed to get the packages back and the code building, but there are missing prefabs everywhere that’s preventing the game from running. I’ll keep trying
The problem is that you are starting the coroutine on a gameobject that you are disabling midway through the coroutine. So, it stops processing.
What you could do is to start the coroutine inside GameManager instead of GameSetUp
In GameManager change StartGame to StartGameRoutine (or whatever you want) and make it private (or internal). I’ve also changed the way you call and wait for the fader
public void StartGame(string subject, int questionsToAsk)
{
StartCoroutine(StartGameRoutine(subject, questionsToAsk));
}
Lastly, in GameSetUp you no longer start a coroutine. Just call the new method directly
public void OnClick()
{
if (string.IsNullOrEmpty(subject))
{
PopUpMessage("You must select a subject to start the game.");
return;
}
if (questionsToAsk < 1)
{
PopUpMessage("You must select a quiz length to play the game!");
return;
}
gM.StartGame(subject, questionsToAsk);
}
Oh for crying out loud, how silly of me! thank you, unfortunately it’ll be a few before I can test as I just discovered that one or more of the files I deleted to get github to accept the upload contained something important and now I have to rebuild all the links.
-UPDATE-
After rebuilding the question base, I am happy to report the bug squashed. thank you @bixarrio for the help!