• 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
1
Question by brodegger.m · Jan 21, 2014 at 01:51 AM · inspector

Inspector not updating value changes

So my problem is that if i change a value of a variable in the source code and save it doesnt update in the Inspector. I know it updates the values when i hit reset on the script in the Inspector, but i see that as an inconvenience.

On the other hand, if i declare a new variable with a set value, the Inspector has no problems to add them correctly.

So basically if i have this:

var speed : float = 3;

and change it to

var speed : float = 5;

it should show up on the Inspector, without having to manually click on Reset.

Comment
Add comment · Show 5
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 · Jan 21, 2014 at 01:53 AM 0
Share

This is by design. You can get around it by making your variables private.

 private var speed : float = 3;

The value assigned in the script for a public variable (and variables are public by default in Javascript), is only used the first time it is declared, or the first time it is added to the script. After that, only the values in the Inspector are used (unless you do something like Reset).

avatar image brodegger.m robertbu · Jan 21, 2014 at 02:09 AM 0
Share

Thanks for your answer, but making the variable private isnt really helpful as it then doesnt show up at all in the Inspector.

avatar image robertbu robertbu · Jan 21, 2014 at 02:13 AM 2
Share

Unity kinda forces you to make a choice. Either you want to modify variables in the Inspectror or you want to modify them in code. Public equals Inspector, private equals code. If having them in the inspector is for debugging, then make them private then turn on debug mode. There is a drop down just to the right of the padlock icon in the upper right corner of the Inspector. Click on the drop down and set the mode to debug. This will allow you to see private variables.

avatar image brodegger.m robertbu · Jan 21, 2014 at 02:47 AM 0
Share

Well see, thats just jumping around the topic. public/private, normal/debug... nothing of that changes anything in regards to my question, especially since editing a value in either Unity or the Editor doesnt change the value on the other end.

You said at the beginning "This is by design", so can i change that design (Inspector) somehow to fit my needs?

avatar image robertbu · Jan 21, 2014 at 02:53 AM 0
Share

I'm not aware of any solution. I've converted by answer to a comment to make it more likely that someone else will find an answer for you.

1 Reply

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

Answer by Jamora · Jan 21, 2014 at 07:55 AM

Robertbu is correct in saying it is by design. This happens because, apparently, the serializedObjects corresponding to our scripts are not updated (at least from values from the field initializers, which are what you're using) after an assembly reload. SerializedObjects are what Unity uses internally to store data which will not disappear during a switch to play mode or during an assembly reload.

Like Robertbu says, public is for inspector, private is code. If you make a variable public, you should modify it from the inspector (when not in runtime, that is. What happens runtime, stays in runtime...). If you make a variable private, you should modify it from code.

There are three ways I can think of to get around this design:

  • Make a custom inspector, and set the value from there, in e.g. OnEnable

  • Add the ExecuteInEditMode attribute to your script and initialize everything in the Awake method. There are consequences to ExecuteInEditMode which may or may not be wanted.

  • Make a constructor, wherein you set the values. (Not recommended in the Unity documentation)

But why get around a good design? Just declare the variable in script, and do all further modifications (setting starting values etc.) in the inspector. For reference types you may have to resort to the Reset method. Set your knife damage too high? No problem, just modify it from the inspector. This eliminates the need to even touch code and cause assembly reloads, which may take a while when your codebase is big.

Comment
Add comment · Show 3 · 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 brodegger.m · Jan 21, 2014 at 03:57 PM 0
Share

But why get around a good design

Well that depends on the point of view^^', as it stands now its a design for me.

Just declare the variable in script, and do all further modifications (setting starting values etc.) in the inspector. For reference types you may have to resort to the Reset method.

That actually sounds good, but if you only declare variables in the script and only modify the values in the Inspector, then Reset turns all values that arent initialized in the script to 0. Which in turn is another problem as you will have to re-enter them or in worst case cant remember some of them.

So after going through your suggestions, it seems that ExecuteInEdit$$anonymous$$ode + OnEnable does what i want it to, so thank you very much and I will be wary of ExecuteInEdit$$anonymous$$ode.

avatar image Jamora · Jan 21, 2014 at 04:03 PM 0
Share

If you don't have anything against pressing Reset per-se, you can make your own Reset function that Unity will call on two occasions: Adding that component to a GO & whenever Reset is pressed from the menu.

The custom Reset callback is meant to give sensible values to your scripts when adding them on GOs.

Unity documentation says one should initialize values in Awake or Start, but I think they should be done in Reset, which is then called from Awake, or Start respectively.

Oh, and if you accept this answer, do mark is as correct by pressing the checkmark below the thumbs.

avatar image brodegger.m · Jan 21, 2014 at 04:25 PM 0
Share

Yes, that seems to be a good compromise for my problem, thanks again.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image avatar image

Related Questions

[CustomInspector] add scriptableobject in a list always null 1 Answer

How to indent EditorGUILayout.BeginToggleGroup() 1 Answer

C# public - dropdown selection? 3 Answers

Making a character's idle animation loop. (model and animation created in blender) 0 Answers

Unity has stopped dynamically displaying changes via the inspector while in the game view. 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