- Home /
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?
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 } }
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.
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).
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? )
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
Follow this Question
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