Load Level

Hi everyone

I have 2 scenes in my project, one is a jungle scene and one a cave scene. My players spawns in the jungle scene.

Now when I’m loading the cave scene all works fine, but when I’m going back again to the jungle scene my player is there two times, because the spawner spawns a player, every time I load the scene. :frowning:

How can I fix this and say that the spawner only spawns a player when I’m loading the jungle scene for the first time? :slight_smile:

Thanks for helping!

Cheers

There are a couple of ways to solve this.

  1. Make an initialization scene as scene 0 and stick your spawner there. This scene only loads at the very beginning of the game to set stuff up, then goes to scene 1. The game should never go back to the initialization scene (scene 0) after it has started.

  2. The PlayerSpawner should know if it has already spawned a player or not. Generally, if an object instantiates/creates a gameObject, that object is in charge of tracking the lifetime of the gameObject. This can be as simple as putting a static reference to the player, or setting a static bool playerExists to true after creating the player, making the spawner a singleton and use DontDestroyOnLoad(), or even trying to FindObjectOfType(typeof(Player)) before instantiating a fresh player.