Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Creature · Jul 04, 2011 at 09:51 PM · variablecacheglobal variable

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!

    Comment
    Add comment
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users

    1 Reply

    · Add your reply
    • Sort: 
    avatar image
    2
    Best Answer

    Answer by Waz · Jul 04, 2011 at 09:58 PM

    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.

    Comment
    Add comment · Show 6 · Share
    10 |3000 characters needed characters left characters exceeded
    ▼
    • Viewable by all users
    • Viewable by moderators
    • Viewable by moderators and the original poster
    • Advanced visibility
    Viewable by all users
    avatar image Creature · Jul 04, 2011 at 10:03 PM 0
    Share

    Are there any side-effects to doing that? $$anonymous$$eaning, will it affect anything else other then make the variable not visible/editable from the Inspector GUI?

    avatar image Waz · Jul 04, 2011 at 10:05 PM 0
    Share

    private will make them inaccessible to other classes (good if you want to keep your code maintainable).

    avatar image Creature · Jul 04, 2011 at 10:07 PM 0
    Share

    So if I want it accessable to other classes, the only place I can change the initial values of the script-level variable is in the Inspector and not script itself?

    avatar image Waz · Jul 04, 2011 at 10:18 PM 0
    Share

    If you mark it NonSerialized, it will not be in the inspector, but other classes will be able to access it. What exactly do you want to happen?

    avatar image Waz · Jul 04, 2011 at 10:20 PM 0
    Share

    A script Foo.JS is actually a class Foo that extends $$anonymous$$onoBevaiour. It's magically wrapped for you by Unity. They are not "script-level", simply class instance variables.

    Show more comments

    Your answer

    Hint: You can notify a user about this post by typing @username

    Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

    Follow this Question

    Answers Answers and Comments

    3 People are following this question.

    avatar image avatar image avatar image

    Related Questions

    Get variable from another object 1 Answer

    Cannot access a variable in another Script. 2 Answers

    Is there a way to make a variable correspond to multiple different scripts? 1 Answer

    Collision object and access to a script variable? 1 Answer

    Accessing variable of script on an instance. 1 Answer


    Enterprise
    Social Q&A

    Social
    Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

    Footer

    • Purchase
      • Products
      • Subscription
      • Asset Store
      • Unity Gear
      • Resellers
    • Education
      • Students
      • Educators
      • Certification
      • Learn
      • Center of Excellence
    • Download
      • Unity
      • Beta Program
    • Unity Labs
      • Labs
      • Publications
    • Resources
      • Learn platform
      • Community
      • Documentation
      • Unity QA
      • FAQ
      • Services Status
      • Connect
    • About Unity
      • About Us
      • Blog
      • Events
      • Careers
      • Contact
      • Press
      • Partners
      • Affiliates
      • Security
    Copyright © 2020 Unity Technologies
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell My Personal Information
    • Cookies Settings
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Explore
    • Topics
    • Questions
    • Users
    • Badges