- Home /
Why is property set not being called in Inspector?
OK, so I've searched all over, and found examples of how to make a public property get called with set so that I can operate on it when it's set, even in the Inspector. I've gotten it to work, and then copied and pasted the working code, changed the name, and it's not working. In the code below, Mass works, but Drag is not being called.
[SerializeField]
private float mass = 0.2f;
[SerializeField]
private float drag = 0.05f;
public float Mass
{
get { return mass; }
set
{
mass = value;
// Update the model
GetComponent<Rigidbody>().mass = mass;
}
}
public float Drag
{
get { return drag; }
set
{
Debug.Log("Setting Drag");
drag = value;
// Update the model
GetComponent<Rigidbody>().drag = drag;
}
}
Can anyone see what the difference is in the code for Drag that's preventing it from being called? No errors. Both variables show in inspector, but changes to Drag are not triggering the debug.
right now, even the mass shouldn't work. because Unity is directly changing the private variables (that's what the [SerializeField] tag is about - so that the unity shows the variables in inspector even though they're private).
so it's changing the private variables directly, therefore it completely ignores the public property getters and setters.
try putting a Debug.Log into the $$anonymous$$ass property setter, and you'll see that it's not being called either.
Actually, it IS being called. As is another property called Size I'm using. The reason it's being called is that it has the same display name. In other words, the private variable mass (lower case $$anonymous$$) shows in the inspector as $$anonymous$$ass (upper case $$anonymous$$) and when it's changed in the inspector the Setter is being called. I know this because I can see the rigidbody's $$anonymous$$ass variable changing with the private variable being changed. I also have Size that sets the scale, and the object gets larger and smaller as I play with that size variable, which is done in its setter. So the process does work. I didn't come up with this, found it online. It's working for size and mass, but not drag or material.
@sh_code BTW, thanks for the reply. And usually, I'd just store what the current value is, and if the current value is different than the old value, apply updates, but I was looking for something friendlier.
Answer by Tanoshimi2000 · Jan 17, 2019 at 04:25 PM
@sh_code So, it turns out that it really didn't work, and those values that were working for Mass were being set elsewhere. I consider this closed, as code that just doesn't work. Apparently, there's no way to catch a property value being set in the Inspector, other than to store the old value and compare the current value to the old value. Thanks to sh_code for making me go back and review this.