Variable Lifespan

Hi All,

Had a few questions regarding Variable Lifespan. Let’s assume I have the following:

Scene 1 - Main Menu
Scene 2 - Play Board

My questions revolve around what happens to variables during scene changes.

For example. Let’s say I click play on scene 1 and now load Scene 2. Scene 2 sets variables etc, allocates an array at the top of a script for the play board etc.

When I leave Scene 2 and go back to Scene 1 after playing, are all my variables still around, or has garbage collection removed them?

If I then click play again on Scene 1 and load scene 2, am I now allocating a second set of variables, such as an array that I load information into?

Do all the events like Awake, Start, etc all get sent again if it is the second time I am now loading Scene 2?

Is there something special I should do when leaving a scene or loading a scene to ensure I am not allocating space for the same variable twice (aka the second time into Scene 2 when I load it).

Thanks for all the help

Chris

PlayerPrefs or DontDestroyOnLoad()

easy as pie or cake which ever you prefer

PlayerPrefs really shouldn’t ever be used for communicating variables between scenes. It’s slow. It slows down legitimate PlayerPref use (AFAIK, every insertion/edit into PlayerPrefs takes a time proportionate to how many keys are stored in it already). It adds a negligible amount of HD use for stand-alones, but for web-players, you have the possibility of a PlayerPrefs Exception. It’s easier to have initialization errors. There isn’t any name-spacing. PlayerPrefs should only be used to communicate data between sessions.

Don’tDestroyOnLoad() is great though. Singletons are also a great solution.

Opening a scene for a second time is the same as opening it for the first time.