Search code examples
c#variablesunity-game-enginescenescene-manager

How to load/get to/from scene variables via loadlevel with scenemanager in unity3d programmatically


Since

Application.LoadLevel(level); 

deprecated and new SceneManager is proposed, i am asking how to use it to load/get simple parameters like string type and in which functions of lifecycle would be best practise?(such as Start, Awake)

SceneManager.LoadScene("resultScene", chosenOptions);

note to simplify: is there an option besides writing new class such as?


Solution

  • Things in Unity work differently than a native mobile app. You don't need pass the parameters to use them in next scene while loading it. Just add a script on empty object in your first scene which has DontDestroyOnLoad in it and save your values in this script. As this object would presist through out the game life cycle, you can store your values in this script. And update them before loading a new scene. and then use them in start of new scene etc.

    You can have a look at callback execution order and choose the method which suits your needs.

    Read more about preloaded scene/object