Script Stops executing after returning from a Fade In Method

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

public IEnumerator StartGame(string subject, int questionsToAsk)
{
      yield return fader.FadeToBlack(waitTime);

      startScreen.gameObject.SetActive(false);
      quizScreen.gameObject.SetActive(true);
      scoreKeeper.CheckReferences();
      quizControl.LoadQuestions(subject, questionsToAsk);
      quizControl.SetNextQuestion();

      yield return fader.FadeToTransparent(waitTime);

      timer.WaitingForAnswer(true);
      timer.StartTimer(quizControl.GetAllottedTime());
}

This will yield the coroutine at the fade in/out and only continue once it is complete.

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.

I just don’t see anything that would just cause it to stop.

If you want, you can upload the project to your repo and I could run it and debug

I’ve forgotten which files are save to ignore and there are over 12k files in the folder. I’ll take it home and set it up on dropbox.

You don’t need the library folder. It will get regenerated. You can also get my .gitignore file from here

It is set up for Unity and will let git ignore all the files that are not required

Got it uploaded project commit

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

OK, this was a fun one to figure out.

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

internal IEnumerator StartGameRoutine(string subject, int questionsToAsk)
{
        yield return fader.FadeToBlack(waitTime);

        startScreen.gameObject.SetActive(false);
        quizScreen.gameObject.SetActive(true);
        scoreKeeper.CheckReferences();
        quizControl.LoadQuestions(subject, questionsToAsk);
        quizControl.SetNextQuestion();

        yield return fader.FadeToTransparent(waitTime);

        Debug.Log(this + "Setting waitingForAnswer");
        timer.WaitingForAnswer(true);

        Debug.Log(this + "Starting Timer");
        timer.StartTimer(quizControl.GetAllottedTime());
}

Now, add a public method to start the game

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!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms