Loading level and DontDestroyOnLoad


To make the checkpoint system in my game, I’m planning to use an object to check all the “checkpoints” the character has passed through, but I’ve got the problem that everytime the character dies I’ve got to restart the level and load it again. So the object must have the DontDestroyOnLoad function.

What I’m having problems with, is when I want to check, If the character died, and the level restarted, or i’m actually loading a new level. I don’t find any way to check this.
Then, if it was a level restart because the character died, spawn the character on the last checkpoint. If he otherwise completed the level, check for the first checkpoint in the new level and spawning him there.

Also, I wanted to know, if the start function, is executed everytime the level is loaded even if the object is not destroyed between levels

Thank you

In your DontDestroy object you just need to put in extra flags/states, eg you have a “bool playerDied” variable, when the player dies you set it to true. When you load the level you can check this variable.

The object’s start() function won’t fire when the new level has finished loading, only when that particular object starts.

There is an OnLevelLoaded call though docs link