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