Using Grid-like singletons but have an issue with Coroutines.
Hi guys,
I followed @Fattie 's guidelines on the Grid.cs (Here) - but having an issue with a Coroutine.
In my game controller, I want to wait a small amount of time before Loading the level complete screen.
//Small delay before level is complete
StartCoroutine (LevelCompleteMinorDelay())
And the function...
IEnumerator LevelCompleteMinorDelay()
{
yield return new WaitForSeconds(0.2f);
SceneManager.LoadScene ("LevelComplete");
}
The error I get is:
MissingReferenceException: The object of type 'GameController' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine)
...
This function is in a script which is attached to the __holder 'not destroyed' gameobject on preload, so I'm not sure why this happens. I prob don't understand enough about coroutines! Any help greatly appreciated :)
Hey buddy .. what is Scene$$anonymous$$anager.
??
Note too that the error reads "The object of type 'GameController' has been destroyed" ... do you have a class called "GameController" ?!?
Answer by Fattie · Feb 04, 2016 at 11:51 AM
I want to wait a small amount of time before Loading the level complete screen.
You do that using
Invoke()
Invoke( "LoadScene", 0.5f);
and then
private void LoadScene()
{
.. your scene loading code ..
}
It's important to NOT use coroutines for this. Use Invoke
Note - this would NOT be in your 'Grid.' static class.
Say you are loading your scene BestScene. This would simply be in a trivial script attached to any object in BestScene. It has nothing to do with "Grid". it's just a function you're running, in that scene - you see?
Cheers mate - I'll look to implement this. btw - Scenemanager is Unity 5.3's way of managing scenes. http://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.html
Answer by aceofpack · Feb 04, 2016 at 07:17 AM
I managed to solve this with the following:
Creating an instance in that specific script at the top:
public static GameController instance;
Assigning the instance to this in Awake();
instance = this;
Calling coroutine like so:
instance.StartCoroutine (LevelCompleteMinorDelay());
Thanks a lot, solved the issue by this suggestion.