- Home /
How can i load may last scene without load duplicate?
hi everybody
i made a main scene and i want character go to another scene and back to the main scene. in the mainscene i have an object and i need it during of the game and i used "Dont destroy OnLoad".when i am loadind my main scene again my object (witch i made it for "Dont destroy OnLoad") is loading again. how can i stop it. i dont want that object be loaded again.
Please Help me Thanks
Answer by Bunny83 · Dec 23, 2012 at 12:46 AM
The easiest and most robust way is to use another scene, a loader scene which you only load once at the start. This scene contains your persistant objects which mark themselfs with DontDestroyOnLoad. All you do in this scene is loading the mainmenu scene immediately at start. Never load the loader scene again since that's the point of it ;)
i did it. i made another scene and in the first it was good but now i have another problem. i am loading this scene and am spawning new character in network. now when i am spawning new character, first character is ok but next character cannot see characters before that it. and next one cannot see 1 and 2!!! what i have to do with this problem now? please help. thanks
Answer by liszto · Dec 23, 2012 at 12:42 AM
if it's an object from your main scene it's normal because when you destroy your scene to load another one, your object is not destroy but the scene always got one in this hierarchy. So when you load this scene again, this scene load it again with her.
To avoid this, you must check in the Awake function if your object already exists in your current scene and if yes just delete it (the new one).
Or other method is to instantiate this object in a script of your main scene (your character is probably a prefab). At start of this script so when you main scene is just loaded, the start function gonna check if your object already exists in the current scene (check with the method of your choice) and if it's yes just don't instantiate it. (I prefer this method ;) )
Answer by ownerfate · Aug 13, 2013 at 12:49 AM
this might be what you were going after, i know it's a very very late comment but if you are still looking.
http://answers.unity3d.com/questions/34185/dontdestroyonload-is-it-intended-behavior.html
duck ♦♦'s script is what i have been searching for... all this time.