- Home /
Keeping Scene States
Hi, i have a scene (scene one) that links to other (scene two) by clicking an object. The scene two has a little minigame, and either you win or lose, links you to the scene one, the thing is that when you´re redirected to the scene one, the previous game advance it´s lost, like the score and the player´s position. I just want to keep that information when changing between scenes, and i´ve search and mentions the use of the DontDestroyOnLoad() but seems not to work, also the use of singletons, wich i don´t understand, so please any solutions..??
Answer by Berenger · Jun 07, 2012 at 02:56 AM
DontDestroyOnLoad() tells a gameObject not to destroy itself when a new scene is loaded. You could use that on a gameObject to which a script containing the score is attached.
You should use a static var. They are accessed through the class, not through an instance of the class, and remains as long as the app is running.
YOU DON'T NEED A SINGLETON, THE FOLLOWING IS JUST FYI
A singleton is a design, a common technique which is define by : There must be only one instance of it, and You must be able to access it from everywhere. (Not an official definition, that's my how-I-understand-it definition). Basically, the trick is to have a public static var that refers the instance of the class, or create it if there is none yet. Like this :
public class MySingleton
{
private static m_Instance = null;
public static MySingleton instance
{
get{
if( m_Instance = null ) m_Instance = new MySingleton();
return m_Instance;
}
}
}
The thing is that if i attach a DontDestroyOnLoad to a gameobject, when my other scenes is loaded, the objects with that method "overlaps" with the recent loaded scene, i used a static var with the score, and it works, but just with the variables, the GUI components and GameObjects loads from the beginning, i want to load them just as the state they was..
Answer by Kinetic · Jun 07, 2012 at 07:04 AM
You can load the minigame with LoadLevelAdditive so it loads on top of the previous scene, that way your main game state will remain.
Besides static vars there is another way to preserve data between scenes, you can modify a prefab directly without instanciating it, this prefab will keep that data between scene changes.
Your answer
Follow this Question
Related Questions
switching scenes 1 Answer
Load specific scene in editor 2 Answers
Is it possible to have multiple scenes saved in memory? 1 Answer
Scene changing and multiple spawn points 1 Answer
Saving current scene in another scene during gameplay? 1 Answer