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 Basyan · Dec 02, 2017 at 05:38 AM · inspectorscipt

Access to a variable in the inspector

Hello. For example, there is such a code.

 public int Health
 {
     get{return _health;}
     set
     {
         //events
     }
 }
 
 private int _health = 100;

How to make the variable Health available in the inspector for the change?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bonfire-Boy · Dec 02, 2017 at 02:09 PM

Health is not a variable, it's a Property, and Properties (like functions) can't be exposed in the inspector.

One option would be to make _health public, then it would automatically be exposed in the inspector. However, presumably the point of your Property is to ensure that your "events" happen when the value is changed from outside the class, and this would remove that protection.

A better alternative is just to use the [SerializeFIeld] attribute on _health. That way you can change its starting value in the inspector, but it retains its privacy as far as its use in code is concerned.

One disadvantage of this approach is that it's still possible to accidentally modify _health directly rather than via Health, from within the class. So an alternative approach that I sometimes take is this kind of thing...

 public int startingHealth = 100;
 private int _healthBacking;
 public int Health
 {
     get { return _healthBacking; }
     set { _healthBacking = value; // other stuff }
 }
 Awake()
 {
     Health = startingHealth;
 }

The point being that because of the names, collaborators (in which I include my future self) are much less likely to use either _healthBacking or startingHealth erroneously. If I'm feeling particularly paranoid I might even call it _healthBackingDONOTUSE. If it's a variable that should never be used directly, why not make that explicit? Similarly, when one changes startingHealth in the inspector one knows exactly what it is one is modifying. When one can, it's better to direct ones collaborators towards the proper usage through careful naming of things, than by adding comments (you can't be sure that they'll spot or read the comments, but they'll always see the names).

It also means that you know that the events that need to happen when Health is set, happen when it is initialised, which can simplify ones set-up code.

Comment
Add comment · Show 2 · 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 Kryzarel · Dec 02, 2017 at 03:36 PM 0
Share

If I could upvote more than once, I would. Great insight right here, totally agree with you :)

avatar image Basyan · Dec 02, 2017 at 04:18 PM 0
Share

Thanks for the extended answer!

avatar image
0

Answer by neosca · Dec 02, 2017 at 08:07 AM

Declare you _health variable as public instead of private. "Health" is your function and "_health" is your variable. You mentioned "Health" as a variable.

 public int _health = 100;

 public int Health
 {
     get{return _health;}
     set
     {
         //events
     }
 }
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 Basyan · Dec 02, 2017 at 04:19 PM 0
Share

Thank you!

avatar image
0

Answer by Adam-Mechtley · Dec 03, 2017 at 10:48 AM

In addition to the backing information provided by @Bonfire-Boy regarding the serialization of fields vs. properties, I want to offer that the recommended way to handle this situation out-of-the-box is:

  1. Make your backing field private and mark it with [SerializeField]

  2. Use your property or otherwise re-validate your backing field data within the OnValidate() callback for your MonoBehaviour. This message is sent whenever data is modified in the Inspector and when the MonoBehaviour is deserialized in the Editor.

You can also use this Asset Store package and eliminate step 2 (with the qualification that it will only handle editing from the inspector, and not also validating upon deserialization).

Comment
Add comment · 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

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

76 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 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 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 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

Script to create instance from in inspector 2 Answers

Unity3D 3.2, MonoDevelop debugger "inspector" problems 3 Answers

How to serialize an array of classes 1 Answer

Get Inspector width 1 Answer

Making a minor change to Default inspector? 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