Setting a active scene on MacOS not working properly

I have a error pop up when i change scenes/load into different scene.

The NOTE here is that this does not pop up on my Win machine and happens consistently on my Mac machine (2021 Macbook Pro 14 M1 Pro)

ArgumentException: SceneManager.SetActiveScene failed; scene 'Childhood Apartment prototype' is not loaded and therefore cannot be set active
UnityEngine.SceneManagement.SceneManager.SetActiveScene (UnityEngine.SceneManagement.Scene scene) (at <87c38cf875804a0cbcc2bd5e75635dd4>:0)

Code that error happens on (tried it with the active code and commented one, same result):

AsyncOperation asyncUnload = SceneManager.UnloadSceneAsync(sceneToUnload);
while (!asyncUnload.isDone) {
    yield return null;
}

// load level
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneToLoad.BuildIndex, LoadSceneMode.Additive);
// asyncLoad.allowSceneActivation = false;
// while (asyncLoad.progress < 0.9f) {
//     yield return null;
// }
// asyncLoad.allowSceneActivation = true;
// if (!asyncLoad.isDone) {
//     yield return null;
// }

asyncLoad.completed += (AsyncOperation obj) => {
    FindSpawnPoint(spawnPoint);

    PlayerManager.instance.SetPlayerPositionAndRotation(playerSpawnPosition, playerSpawnRotation);
    player.GetComponent<NavMeshAgent>().enabled = true;

    Debug.Log($"Scene to load is ready?: {obj.isDone}");

    SceneManager.SetActiveScene(SceneManager.GetSceneByBuildIndex(sceneToLoad.BuildIndex));
};

Console output:

If anyone has any idea why this happens, please!

Delay the SetActiveScene call by one frame eg by starting a coroutine that yields null first. The loading complete event may technically still be part of the loading process.

Thank you for the suggestion, tried this with a few variations (WaitForEndOfFrame(), WaitForSeconds(1)) and i still get the same error.

Please post your updated code.

Either you’re not waiting correctly or the scene doesn’t get activated. Just yielding the AsyncOperation of LoadSceneAsync should be enough to wait until you can activate the scene.

You could check SceneManager.GetSceneByBuildIndex(index).isLoaded to check if the scene is ready to be activated.

I wrap all my scene loading (and I do a LOT of additive and multi-scene loading, plus manually specifying the active scene, etc.) with this thing and never again write specialized code:

Thank you for all of the suggestions…
I tried most of them, then i tried to set the active scene on button press (to rule out “timing” or scene loading event duration) and still got the same error!
My suspicion is that it is something to do either with wrong project settings or player settings for mac machine…
Since it works on Win machine i will ignore the issue for now and write it off to “unity macos quirk”.

I’ve been using Unity on mac for over 15 years and always also exported for windows. I can’t think of a project or player setting that could influence scene loading or anything about scene loading that is different on mac vs windows.

What’s much more likely is an ordering issue or race condition that triggers some bug in your code. Ordering of messages can be pretty stable from the same project on the same machine but is not guaranteed and can change between platforms or when you build on a different machine. This can easily lead to bugs that you don’t discover for a long time because it just happens to work in your own project instance. Timings can also be pretty different between platforms and cause races that mostly go one way on your machine to go the other way on other platforms.

As I said, if you post your current code we can help verify and narrow down the root cause of your issue.