- Home /
Pass values between scenes
Hello,
Is it possible to pass values (an instance of class could be great) betweens scenes ? And genraly is it possible to define some values and access them during game (like a repository) ?
Thanks for your help.
Answer by diddykonga · Jul 03, 2012 at 08:17 AM
Well one of the best methods is to put in DontDestroyOnLoad() in the gameObject's code, so that way it will persist through out scenes. :)
Answer by Wolin · Jul 03, 2012 at 11:05 AM
You can create a game object with DontDestroyOnLoad() or simply make a static manager class which holds values you need (for example a score).
public class ScoreManager {
private static ScoreManager scoreManager = null;
private int score = 0;
public static ScoreManager getInstance() { if(scoreManager == null) { scoreManager = new ScoreManager(); }
return scoreManager; }
public int getScore() { return score; }
public int setScore(int score) { this.score = score; } }
sorry had to wake this up again .. but does the static manager need to be in hierarchy in every scene? Or the first loaded screen would be enough to place static manager?
If you wanted to use a static manager like this, if its not a $$anonymous$$onoBehaviour then you can just create it once (In Code), but if it is, then either yes it needs to be in the heirechy for each level, or put DontDestroyOnLoad() in it.
Answer by _EdzUp_ · Jul 03, 2012 at 12:55 PM
If you wanted to pass things like scores etc you could also use PlayerPrefs to do it, there is a limit on the size of the amount stored for the webplayer of 1Mb though.
Thanks for your help. But it seems that it's like a Dictionary with key/value and value are string. As you said and you're certainly right, it is perfect for score or configuration. But at this time I need to store complex data.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073621im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Static Value? 1 Answer
recharging value? 0 Answers
Static Variable Doesn't Change ? 0 Answers
Calling non-static from static function C# 2 Answers
Should animated game object be market as a static? 0 Answers