Search Unity

whats the best way to carry data from scene to scene

Discussion in 'Scripting' started by drazil austin, Dec 28, 2010.

  1. drazil austin

    drazil austin

    Joined:
    Dec 16, 2009
    Posts:
    236
    i just wanted an opinion on what would be the best way to carry data (like a string) from 1 scene to another. i am making a mazemaker game and the mazes will be savable to a database. i need a good way to carry the selected maze to play from the maze selector to the scene that loads the maze.
     
  2. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    Common methods for causing data to persist between scenes include:

    - Associating the data with a game object that's been made persistent using DontDestroyOnLoad().
    - Using static variables.
    - Using the PlayerPrefs class.

    If you want the game to 'remember' the last selected maze between runs of the program anyway, PlayerPrefs might be your best bet. Otherwise, either of the other two options should work.
     
  3. drazil austin

    drazil austin

    Joined:
    Dec 16, 2009
    Posts:
    236
    i was thinking of using player prefs before posting this thread but i just wanted to see if there was a better way. thanks
     
  4. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Honestly, if it is a small amount of information, then playerprefs is the best solution. Fast save, fast load, love that little baby.
     
  5. mbfloyd

    mbfloyd

    Joined:
    Mar 6, 2009
    Posts:
    12
    you could also write a simpleton class that never needs instantiation. This will allow all coded to asses it by its file name, but it will not persist through game sessions. (ie, if you close your browser and comeback later.

    persistant_data.js
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. static var i_score : int = 0;
    5.  
    6.  

    score.js
    Code (csharp):
    1.  
    2.  
    3. function Start () {
    4.  
    5. persistant_data.i_score += 100; // adds 100 to i_score stored with in persistent data
    6.  
    7. }
    8.  
    9.