So I load a new scene and have a coroutine that should do some work once the next scene is loaded, the scene is loaded and I can see that in play mode and the editor says it’s the new scene. Yet when I call FindObjectsOfType it returns objects from the previous scene. So I set it to waituntil active scene is x scene and logged the active scene and the active scene from SceneManager.GetActiveScene() is always the previous scene that was unloaded?
I load the scene this way:
public static IEnumerator LoadAsyncSceneFromArgument(string s)
{
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(s, LoadSceneMode.Single);
// Wait until the asynchronous scene fully loads
while (!asyncLoad.isDone)
{
yield return null;
}
SceneManager.SetActiveScene(SceneManager.GetSceneByName(s));
}
This is the method that never gets the new scene which is clearly the active scene as reflected by play mode, scene mode and the hierarchy tab:
public IEnumerator RunMultiSceneCutscene(List<CutsceneToRun> c)
{
cutscenesToRun = c;
// Using a for loop to iterate through the list of cutscenes
for (int index = 0; index < cutscenesToRun.Count; index++)
{
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE {cutscenesToRun[index].cutsceneName} in scene {cutscenesToRun[index].sceneName}");
#endif
bool done = false;
var cutsceneData = cutscenesToRun[index]; // Access the current cutscene by index
// Check if this is the last cutscene in the list
if (index == cutscenesToRun.Count - 1)
{
activeSaveData.LoadTemporaryPlayerPosition = true;
}
// if (cutsceneData.sceneName != currentScene)
if (cutsceneData.sceneName != SceneManager.GetActiveScene().name)
{
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Requesting scene change to {cutsceneData.sceneName} from {currentScene}");
#endif
AreaExit areaExit = FindObjectOfType<AreaExit>();
yield return areaExit.ExitSceneWithoutLoadScreenUsingSaveData(cutsceneData.sceneName);
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Scene change to {cutsceneData.sceneName} from {currentScene} complete");
#endif
done = true;
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Waiting for scene to load");
#endif
yield return new WaitUntil(() => isSceneLoaded == true);
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Scene loaded");
#endif
}
// else
// {
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Running cutscene {cutsceneData.cutsceneName} in scene {cutsceneData.sceneName} in scene: {SceneManager.GetActiveScene().name}");
#endif
yield return new WaitUntil(()=>SceneManager.GetActiveScene().name == cutsceneData.sceneName);
var cutScenes = FindObjectsOfType<CutScene>();
CutScene cutScene = null;
for (int i = 0; i < cutScenes.Length; i++)
{
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Checking cutscene {cutScenes[i].name}");
#endif
if (cutScenes[i].name == cutsceneData.cutsceneName)
{
cutScene = cutScenes[i];
}
}
if (cutScene == null)
{
Debug.LogError($"Cutscene {cutsceneData.cutsceneName} not found in scene {cutsceneData.sceneName}");
}
else
{
// cutScene.ExecuteCutSceneWithCallback(() => done = true);
yield return cutScene.ExecuteCutSceneCoroutineWithCallback(() => done = true);
}
// }
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Waiting for done to be marked complete");
#endif
yield return new WaitUntil(() => done == true);
#if UNITY_EDITOR
Debug.Log($"MULTICUTSCENE Done marked complete");
#endif
}
yield return null;
}
In the hierarchy the new scene LD04 is shown and it’s also displayed and running in scene and play modes.
Meanwhile, the method is logging that the active scene is LD03 (the previous scene)
And the coroutine just hangs at the waituntil scene name equals active scene name:
yield return new WaitUntil(()=>SceneManager.GetActiveScene().name == cutsceneData.sceneName);