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…??
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;
}
}
}
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.