Does loadscene reset the scene immediately?

Hi! I have the following code:

if (playerhealth == 0)
{
SceneManager.LoadScene("GameOverScene");
Debug.Log("Game ended");
}

When I run the code, it seems that “Game ended” is printed after the load scene. I don’t understand why this will happen as aren’t loadscene reset everything and interrupt the script immediately? Thanks!

What a mystery… and that can only mean… time to start debugging!

NOTE: as per the docs, scenes do not load until end of the current frame.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

2 Likes

No, code doesn’t just interrupt itself like that.

Loading the same scene, while used as a reset, isn’t a reset in the strictest sense. It’s simply loading a new instance of the same scene, and unloading the previous instance. The previous instance will still execute in full to the end of frame, before the new scene is integrated in the next frame.

1 Like

Yes! And you can actually see this by calling Debug.Break(); at the same point you reload the scene.

That pauses the editor, so go study the Hierarchy window: you’ll notice your scene still all present and study-able (although it is old and doomed), but also a fresh copy ready to step in.

Screen Shot 2024-08-21 at 5.31.33 PM

Use booleans if you need to get out of dodge and make sure other stuff doesn’t happen.

2 Likes

Thank you for your reply! I have a few additional questions:

  • By scene unloaded, do you mean that every instances, objects and scripts (and the fields and functions in the scripts) are deleted ?
  • By loading the new scene, are the gameobjects and instances loaded in like how I clicked the “Play” button in the editor window?
  • Is there any way that I can check whether the previous scene has full unloaded before start instantiating the next scene? As I’m creating a wave system, when I restart the scene, some error such as NullReferenceException appeared, and I’m suspecting that they are caused by this ambiguous state of previous scene not fully unloaded and the new scene not fully loaded.

Thanks again!

Destroyed would be the better way of putting it. Imagine loading a scene as Instantiate()-ing everything in the scene asset on disk, and unloading a scene as Destroy()-ing everything.

As above, it’s taking everything from the scene asset file and and copying that into memory for use in runtime. So in most cases, that will be how the scene is as you enter play mode.

Loaded scenes are copies of scene assets, effectively. Hence why you can load the same scene more than once. It’s just multiple copies.

Don’t suspect or assume, test. The first step for any null-ref is to determine what is null, then work out why it is null, and only then can you take steps to rectify that.

2 Likes

If you find yourself EVER saying this when coding:

That’s like suspecting “Hey, I think I left my glasses in the kitchen!” and then posting about it to the world.

It’s not useful. All we can say “Great! Let us know if they’re in the kitchen.”

Use debugging to TEST your suspicions. Craft actual specific code to TEST your theory. Now use the scientific method and test your ANTI-theory, eg, test for the negative hypothesis if possible.

When you have corralled it to the boundaries that you can imagine and craft given what you know about your game, only then can you post the findings of your testing experiments and confirm “Hey, this is happening, I did this, this, this and this, here’s my code, what is going on?”

1 Like