- Home /
Can a script be loaded to a next scene?
I was wondering if it possible to have a script loaded on the next scene of the one it was originally in. In my game, i have a certain script on the Player that he needs throughtout the whole game. Thanks!
Answer by KellyThomas · Dec 19, 2013 at 10:55 PM
Any GameObject can be marked as persistent between scenes by calling [`DontDestroyOnLoad()`][1].
If you want the rest of your Player to be "reset" between scenes (as I expect you do), then you could attach the player script to a persistent object just before scene transitions, then retrieve it once the scene load is complete.
An alternative option might be storing some values is static variables.
[1] http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
Answer by WesterlyCarrot9 · Dec 19, 2013 at 11:21 PM
Well i actually dont want to keep the Player but a certain script that is attached to him in the Intro scene.
Answer by RyanZimmerman87 · Dec 19, 2013 at 11:31 PM
Hmm looks like the Unity website having some issues today, cant post comments here or edit my existing posts elsewhere.
But yes you can do this very easily with the don't DontDestroyOnLoad();
However, if you are new to programming or Unity and don't have a really good reason to not destroy it, I would avoid this. A better solution would be PlayerPrefs or using a public static variable which will survive scene transitions within the project until you exit the game I think.
This should be a comment really since DontDestroyOnLoad() is the correct answer but looks like I can only do answers at the moment.
Your answer
Follow this Question
Related Questions
Convert this line of javascript to C# (easy) 1 Answer
Character falls through the ground 1 Answer
How to start a NEW scene in my main menu. 1 Answer
Distribute terrain in zones 3 Answers