I experienced an issue with my game where if I had killed one or more enemies in scene one on my way to the portal to scene two, on returning to scene one I was receiving a load of null reference errors related to the ‘if health is 0, run the die method’ that is called during the restore state method in the health class.
After spending a while tracing and trying to resolve this, I ended up inserting a new waitforseconds to allow everything to load in correctly before trying to restore the saved states.
My Transition method ended up as below.
IEnumerator Transition()
{
if(sceneToLoad < 0)
{
Debug.LogError(“Scene to Load has not been set”);
yield break;
}
DontDestroyOnLoad(this.gameObject);
Fader fader = FindObjectOfType<Fader>();
yield return fader.FadeOut(fadeOutTimer);
// Save Progress before loading next scene
SavingWrapper wrapper = FindObjectOfType<SavingWrapper>();
wrapper.Save();
// Load next scene
yield return SceneManager.LoadSceneAsync(sceneToLoad);
/* Pause to allow all game objects to load in correctly before running the wrapper.load() method.
* This is to stop null reference errors encountered when dealing with dead enemies */
yield return new WaitForSeconds(waitBetweenScenesTimer);
// Load Progress before updating player position etc.
wrapper.Load();
// Update Player position in the new scene
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
yield return new WaitForSeconds(waitBetweenScenesTimer);
yield return fader.FadeIn(fadeIntimer);
Destroy(this.gameObject);
}
Is this an OK way to deal with this issue or is there a better method that I should have used. This resolution does appear to work OK but obviously does introduce another delay between scenes.
Thanks in advance