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 tuf91497 · Feb 03, 2019 at 07:59 PM · scripting problemeditorserialization

How does the Unity Editor actually add variables to public/protected fields when you drag objects onto the field?

So in my case I'm using the Oculus SDK. Whenever you want to grab an object with the Oculus SDK you have to add this "OVRGrabbable" script, a rigidbody, and a collider to the object. Then you have to drag the object's collider into this protected "Grab Points" field in the OVRGrabbable script, which is just a protected array of colliders that is exposed to the designer in the editor with [SerializeField].

My issue is that I can add my colliders in the editor because it is a private serialized field, but I can't do it in script.

My question is, what command is the Unity Editor actually running when you drag an object onto a public or serialized field? GetComponent<>() or GetComponents<>()? Or is it something else? How can I mimic this behaviour in script for private serialized fields?

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
1
Best Answer

Answer by xxmariofer · Feb 03, 2019 at 08:45 PM

why not just add create setters?

 private int ovrVar;
 public int OvrVar{ get { return ovrVar; } set { ovrVar = value } }
Comment
Add comment · Show 4 · 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 tuf91497 · Feb 03, 2019 at 09:09 PM 0
Share

This worked, thank you. The one difference, and there doesn't seem to be a performance cost of any kind so it isn't a big deal, is that now my Grab Points array (as shown in the editor) is the length of the my compound collider array. That makes sense. But, when I dragged my compound collider onto OVRGrabbable in the editor, my Grab Points array was only 1 index. With this method it is 1100. It's just curious.

avatar image Owen-Reynolds · Feb 03, 2019 at 10:24 PM 0
Share

This is the same as making one public variable. Everyone else has direct, unrestricted access to it, either way.

If you decide you need a real getter/setter later, it's easy to turn your public variable into one (that's the reason they were invented).

avatar image Glurth Owen-Reynolds · Feb 03, 2019 at 11:02 PM 0
Share

Easy? I've been perpetually annoyed by this for years when using Unity editor! Unity editor default inspectors will NOT use/display the getter/setter, it will only use the serialized variable. So, if your setter actually DOES stuff, that NEEDS to be done when the variable changes, this code is not called when changing the field in unity editor. One $$anonymous$$UST define one's own custom inspector in order to ACTUALLY use the getter/setter. ( I'll admit I haven't re-tested this in a while, has it changed? )

avatar image xxmariofer Owen-Reynolds · Feb 03, 2019 at 11:08 PM 0
Share

what do you mean by a "real getter/setter"? is there any advantage/difference of having a method, rather than auto-implemented propertiy? and they are not the same as public fields, you can mark the set for example as private if you want to make sure they are not modified from outside , but he was asking for an unrestricted way of accessing the var. also as far i understand they are not binary compatible with fields, so you might have Binary Breaking Changes issues, but i might be wrong so please correct me.

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

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

Seperating editor and runtime data with ScriptableObjects 1 Answer

Saving data to local Device 1 Answer

How can i make both two cameras to follow the player but only one with control on player ? 0 Answers

'Unsupported type' error in custom editor script 4 Answers

Trouble setting the object reference in a property drawer 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