Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 John Bye · May 23, 2013 at 01:33 PM · c#serializationvariables

Variable values not updating when script edited

I have several public static variables in a C# script, which aren't listed in the inspector. When I edit the script in MonoDevelop to change the values of those variables, the new values aren't being reflected when I run the game in the editor, although any other changes I make to the script at the same time work as expected.

Apparently the values of public variables are cached by the Unity editor when it loads the scene, overriding whatever changes I make to them in the script from that point on, until I shut down the editor and restart it.

I removed the [System.Serializable] from above the class definition and tried using [System.NonSerialized] on the variables within it, but that didn't make any difference. As noted above, none of these values are listed in the inspector anyway, so I don't understand why they don't just always reflect what's in the script, as you'd expect them to.

Is there any way to stop Unity overriding variable values like this, or to force it to flush whatever hidden cache it's pulling them from without having to restart the editor or reload the project every time I make a change?

NOTE: I can't really change the variables to private or non-static, or set their values in the Start script instead of where I declare them, as I've seen suggested in previous threads, as they're being used to initialise variables in other scripts for export to an external file.

Any help would be much appreciated.

Comment
Add comment · Show 4
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 robertbu · May 23, 2013 at 01:36 PM 0
Share

The "caching" is done so that users can modify the values in the Inspector. There is a gear with an arrow on the right edge of each component. In the inspector you can select "Reset" to get that specific component to reset its values to the current ones defined in the script. Also you could initialize them in Awake(). That way they would be available in Start().

avatar image John Bye · May 23, 2013 at 02:01 PM 0
Share

Thanks, but I tried using the Reset option (and even removing the script component from the object entirely and putting it back again), and that didn't work either. Also, as noted above, these variables don't actually appear in the inspector, so there's no reason Unity should be caching them really.

I've seen NonSerialized working as expected in a fresh script on another person's machine, and there's a public static variable in one of my scripts I'm having problems with that updates properly when I set it to be NonSerialized. That variable used to be a float, but I changed it to an array of floats this morning, which presumably would have flushed or invalidated whatever data Unity had stored for it. All the other public static variables in that script (and another one in the same scene) don't update though, even when I set them as NonSerialized.

So it may be a data corruption issue. Does anyone know if there's a way I can find and flush / delete whatever cache Unity is using for this and see if that clears the problem?

avatar image robertbu · May 23, 2013 at 03:00 PM 0
Share

I missed the boat on my answer. I've converted it to a comment so you are more likely to get an answer. It sounds like a Unity bug.

avatar image Overdroid · Dec 01, 2014 at 04:03 AM 0
Share

This is still happening. I've been trolling the forums and question boards for hours looking for a solution. There is none. Shut Unity down and restart it -> Nothing. Shut $$anonymous$$onoDevelop down and restart -> Nothing. Change Variables to private and back -> Nothing. Reset the Script component -> Nothing. SHUT DOWN THE ENTIRE CO$$anonymous$$PUTER AND RESTART -> NOTHING.

UNITY SI$$anonymous$$PLY WILL NOT SHOW THE CHANGED VARIABLES FRO$$anonymous$$ $$anonymous$$Y SCRIPT IN THE EDITOR AT ALL.

I know Unity probably works perfectly well for you and you think you have a solution, but this is a bug in Unity.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ctwheels · Oct 28, 2014 at 09:44 AM

I know this is like a year later, but you can set the variable in question to be private, save your script, then change the variable back to public and save it again. It worked for me.

So,

 public float myNum = 10;

to

 private float myNum = 10;

and back to public after you save it.

Comment
Add comment · Show 1 · 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 crummy · Jan 11, 2015 at 07:06 PM 0
Share

In Unity 4.6 this doesn't seem to work for me. Gear > Reset did, however.

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Serialize Variables from Items in an Array 1 Answer

Reference to value type 1 Answer

prevent field json serialization in 5.3 0 Answers


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