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.

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;
 }
}

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.

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. :slight_smile: