- Home /
character doubles when returning to the main scene
hi. I would like to ask on how will I be able to avoid multiplying my characters after loading a level. My character will travel to scene0 to scene1. After finishing scene1 it will return again to scene0. But after it returns, it doubles... what do you think is the problem
Tell us how the "Player" got into Scene 0 to start with. Is it just a GObj in your Hierarchy when the scene is loaded, is it a prefab one of your scene scripts is instantiating, etc. I guess it's also worth clarifying does your 'player' have a bunch of custom classes/scripts/variables associated with it (or is it just a happy cube)?
Answer by HolBol · Feb 06, 2012 at 05:19 PM
I'd check with a script to see before the scene has loaded whether there is already a character there, and if so, delete it, and if not, just carry on- so this way you'll only have one.
Answer by kevork · Feb 06, 2012 at 07:42 PM
You likely are instantiating your character in a script's awake or start function. The object this script is attached to is being destroyed and reloaded when you load the second scene, triggering the awake & start again. You have a lot of options for how to fix this problem. Either don't have the object that instantiates the character be loaded on every scene or check for an existing character before you instantiate, and don't instantiate it the second time.
Answer by nightbane30 · Feb 08, 2014 at 07:17 PM
If I were you, I would use destroy. Also, make sure you don't have DontDestroyOnLoad on the player during the scenes, as this would cause it to duplicate itself in the next scene.
I disagree. It's likely there are many scripts and variables on Player that could easily have changed in scene 1 (paid gold for item, level'd up, earned XX points, whatever) and if we destroy on return to scene 0? Bad mojo. Player is usually going to have DontDestroyOnLoad
The real issue is how the permanent "player" got into scene 0 in the first place
It does sound like a DontDestroyOnLoad problem. If you use it, you have to think carefully about how to spawn only once. One solution is to use Destroy in Awake if you see you're an extra player. Or, don't start the player in any scene, but have each master "sceneScript" instantiate if needed. Or put the player in the intro scene, which you can never go back to.
getyour: I sometimes find I have to create the player differently for some levels -- rigidbody for the space level, charController for land, diff movement script for the boardGame-style level… . Sometimes having the player naturally be born/die with the level, and a dontDestroyOnLoad playersStats object (for gold, level…) is easier.
True. Or, couldn't you just duplicate Scene0 but remove the player gameObject, and just have everything else entirely the same? It's not the most memory saving way, but it would work.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Selecting scene for your main menu 1 Answer
Problem with click to display information 1 Answer
Save game objects to new scene 0 Answers
How to load a scene on collision 1 Answer