Character duplicates herself when re-entering the home level

Hello- so I just applied this script to a general object in the home level and set it as a trigger

function OnTriggerEnter(other : Collider) { Application.LoadLevel("scene 2"); }

and made another level with a another general object the same way to go back to the home level.

I also put this on the player, a npc that follows her throughout the game, and the main camera. I deleted the main camera in the second level to keep the one from the home level.

function Awake () { DontDestroyOnLoad (this); }

The player, npc, and camera transport perfectly to the second level, but when I hit the object in the second level to go back home, the player, npc, and camera, duplicate each time! How do I make sure the game retains the original player, npc, and camera no matter which level I enter or re-enter? Also, how do I allow any item they might holding (and only that item) to transport with the player or npc if they are holding it?

Thanks for any help

The way I handle this kind of situation is to have an extra scene with just the player (or object that needs to persist through scene loads)

What you have

  • 2 scenes: `Scene A` and `Scene B`.
  • `Scene A` has a `Player` and the environment.
  • `Scene B` just has the environment.
  • When `Scene A` loads it creates the `Player` and tells it `DontDestroyOnLoad`.
  • `Scene B` is loaded and the `Player` comes along for the ride.

My approach

  • 3 scenes: `Scene Loader`, `Scene A`, and `Scene B`
  • `Scene Loader` has a `Player` and a script to load the next scene
  • `Scene A` and `Scene B` just have the environments
  • When `Scene Loader` is loaded it creates the `Player` and tells it `DontDestroyOnLoad` then immediately loads `Scene A`
  • `Scene A` is loaded and the `Player` comes along for the ride.

That, or you could try useing one gargantic scene =). That is exactly what I do sometimes.