Caching global vareiables? (n00b question)

Does Unity cache global variables between test plays? I notice that when I create a global variable (Outside of any function/method) and initialize it in the same line as the declaration, run the game, then change it and run the game again, the new value doesn’t take effect. (I do stop the test play before I edit it and save, so it’s not like I do this while the game is running) Is this intended behavior? If so, how do I make it recognize the change?

I know using global variables is bad coding practice, I’m going through a tutorial that has code that uses them. Just want to understand why the engine is acting the way it is.

To duplicate:

  • Create new game.
  • Add an empty game object.
  • Create the following script and attach it to the object:

    `var num : int = 7;

    function Update ()
    {
    Debug.Log(num);
    }
    `

  • Save script and run game.
  • 7 gets printed to the log as it's supposed to.
  • Stop test play.
  • Edit the script again and change 7 to 8.
  • Save script.
  • Run the test play again.
  • 7 still gets printed to the log, even though you've changed the value of the variable to 8.

    Unity version: 3.3.0f4

    OS: Windows 7

    Thank you!

There are no global variables. What you are seeing is variables visible in the Inspector being saved. If you don’t want this for a variable, mark it private or NonSerialized. See this question.