Coroutine couldn't be started because the the game object is inactive on a persistent object

Hi all, I appreciate there’s lots of similar questions to this, but I’m getting the above error. What sets mine aside from the other questions I’ve seen is the fact that the coroutine I’m calling is bound to a monobehaviour which is a singleton and flagged as DontDestroyOnLoad (and it appears under DontDestroyOnLoad in the hierarchy).

The error occurs when I restart the level, which calls TriggerReloadCurrentScene

Here’s the code:

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

public class GameLoadingManager :  Singleton<GameLoadingManager>
{

	public void LoadSceneAysnc (string scene)
	{
		CentralEventBroadcaster.BroadcastOnLevelStartedLoading ();

		if (scene != null) {
			StartCoroutine (AsynchronousLoad (scene, () => {
				Debug.Log ("Finished Loading Scene.");
				CentralEventBroadcaster.BroadcastOnLevelLoaded ();
			})
			);
		} else {
			Debug.LogError ("No Next Scene Defined");
		}

	}

	////////////////////////////////////////////////////////////////////////////////////////////
	/// Asychronously loads a level
	IEnumerator AsynchronousLoad (string scene, Action onComplete)
	{
		AsyncOperation ao = SceneManager.LoadSceneAsync (scene);
		ao.allowSceneActivation = true;

		// Note that if allowSceneActivation is FALSE, ao.isDone will stall at 90%
		while (!ao.isDone) {
			float progress = ao.progress;//Mathf.Clamp01 (ao.progress); 
			Debug.Log ("Loading Progress: " + (progress * 100) + "%");
			yield return null;
		}

		Debug.LogWarning ("Done loading");
		// Call action on complete
		onComplete ();
	}

	////////////////////////////////////////////////////////////////////////////////////////////
	// Reloads current scene
	public void TriggerReloadCurrentScene ()
	{
		// Ensure timescale is reset
		Time.timeScale = 1.0f;

		//SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
		LoadSceneAysnc ( SceneManager.GetActiveScene().name);
	}

	////////////////////////////////////////////////////////////////////////////////////////////
	/// Quit the game. It will quit the editor (when in editor) or close the app when running standalone
	public void Quit ()
	{
		#if UNITY_EDITOR
		UnityEditor.EditorApplication.isPlaying = false;
		#else
		Application.Quit();
		#endif

	}

	////////////////////////////////////////////////////////////////////////////////////////////
	// Notifies other scripts to do whatever they need to do as we are about to load
	public void NotifyLoadingNextLevel ()
	{
		CentralEventBroadcaster.BroadcastOnLevelStartedLoading ();
		StartCoroutine (Transition ());
	}

	////////////////////////////////////////////////////////////////////////////////////////////
	IEnumerator Transition ()
	{
		HUDManager.Instance.FadeToBlack ( 1.5f);

		yield return new WaitForSeconds (3.0f);
		
		LoadSceneAysnc ("Level1");
	}
}

Before StartCoroutine (AsynchronousLoad etc) is called, I’ve checked for the number of GameLoadingManagers in the scene and there is only one so I’m at a loss to explain this. Any ideas?

Thanks
Ant

It seems that you have the Singleton instance inside your scene. When you reload it, Unity recreates the singleton and deletes the old reference. There are some solutions you can make, such as put your singleton in a scene that will never be reloaded and put no other instances of this Singleton inside any other scene, but this will make your game not testable by that scene if it depends on the Singleton.

I struggled with this issue for some time in the past and end up creating a Singleton implementation, which I posted here: http://bit.ly/2fe6kFb

There I explain all the problems I faced and finish with an implementation that I use in almost all my projects. Needless to say that I’ve made several changes based on the community feedback. So it’s well tested :slight_smile:

Sorry if it didn’t quite answer your question.