Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Global Variables or passing data between levels

Discussion in 'Scripting' started by mldesanctis, Oct 5, 2010.

  1. mldesanctis

    mldesanctis

    Joined:
    Sep 30, 2010
    Posts:
    40
    I am just starting out with Unity and I feel like I am getting pretty far, fairly quickly. There is one thing that I do not seem to be grasping: How or where do you store information that should remain persistent? If you Application.LoadLevel(0), and then the player loses a life and accumulates a score of 1000, how do you inform level 1 of the data that was previously held in level 0 when you call Application.LoadLevel(1)?

    Is there some sort of repository for information at the Application level that I am not seeing?
     
  2. Rbn_3D

    Rbn_3D

    Joined:
    Sep 30, 2010
    Posts:
    17
    Last edited: Oct 5, 2010
    AndrewBilotti likes this.
  3. Hoff

    Hoff

    Joined:
    Jun 20, 2010
    Posts:
    223
    Take a scene such as a splashscreen or menu that loads near the start of the game, set DontDestroyOnLoad() so that it never unloads, and attach your globals to a empty game object in that scene.
     
    URPian, Fractals00V, Ruduen and 3 others like this.
  4. mldesanctis

    mldesanctis

    Joined:
    Sep 30, 2010
    Posts:
    40
    Thanks Rbn_3D. PlayerPrefs is a great solution and was very easy to implement.

    Hoff, I am also interested in the DontDestroyOnLoad() method, because this is more along the lines of what I was initially thinking. What happens to the GameObject when you call it? Or more appropriately, where is it? Is it added to the hierarchy of all loaded levels, or only the currently running one? What if I use LoadLevelAdditive()?
     
  5. Dougxing

    Dougxing

    Joined:
    Mar 13, 2008
    Posts:
    90
    If you're just keeping track of values, you can use static variables in your script. So, for example, if in a script GameState you declare

    static var score = 0;

    (in javascript)

    GameStats.score could be accessed from any other script, in any scene and the value will persist between scenes.
     
    jacksonmeade, Ylex, PsyRot and 2 others like this.
  6. nw-admin

    nw-admin

    Joined:
    Jun 17, 2010
    Posts:
    53
    Thanks Dougxing !
     
  7. mldesanctis

    mldesanctis

    Joined:
    Sep 30, 2010
    Posts:
    40

    That is exactly what I was looking for Dougxing. Thanks.
     
  8. scarface

    scarface

    Joined:
    Mar 14, 2011
    Posts:
    2
    Very helpful Dougxing. Thanks!
     
  9. Markcreator

    Markcreator

    Joined:
    Mar 26, 2013
    Posts:
    2
    Thanks! Herlped me out alot. :D
     
  10. marinl

    marinl

    Joined:
    Mar 23, 2013
    Posts:
    10
    You can create an object in first scene, attach a sript to it and sen value of variable in this script. For example if you want to send user login script attachet to game object that will survife after second scene load should be:

    Set userLogin in first scene and read in second.

    Complete example can be found in my blog http://unity-tutorials.blogspot.com/. Listing 20 and 21.
     
    MarcSev likes this.
  11. PedroH

    PedroH

    Joined:
    Nov 8, 2012
    Posts:
    3
    Great Help !!
     
  12. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
  13. softnax

    softnax

    Joined:
    Mar 20, 2015
    Posts:
    6
    have both scripts in the same Directoy (Not sure if this is nessesary depends on the IDE & Project linkage of files)
    set your shared variables as Static Public
    use

    ScriptName.VariableName = for assignment from another script.
     
  14. AndrewBilotti

    AndrewBilotti

    Joined:
    Jan 6, 2016
    Posts:
    3
  15. dadyno

    dadyno

    Joined:
    Jul 25, 2017
    Posts:
    1
    Thanks, I used this to stop my background music from starting all over again when loading next level of my game.
     
  16. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    129