how do i call a specific function when property is changed?
been trying get/set properties, then get/set functions, but no luck
my debug.log is never run in the set part
i’m new to c# , just been looking over sample code
not sure best way to do this
I don’t believe you can. About the closest I’m aware of is OnValidate(), though that doesn’t tell you what changed.
1 Like
just seems so odd that you have to compare values to find out what one has changed, when one knows what one has changed…
Why do you care which one has changed? What is it that you’re trying to achieve?
when i change a color in the inspector,
I want to run a function that changes the color of multiple objects.
right now i’m trying a custom editor script,
but i can’t capture the color picker values
EditorGUILayout.ColorField(“color”, myColor.colorValue);
Debug.Log(myColor.colorValue);
value doesn’t change when i change it, so i’m still doing something wrong… 
I manage to get it working without a custom editor through the update function, but I thought it would be better to hook my function directly to the colorField change … now i’m just really confused
EditorGUILayout.ColorField(“color”, myColor.colorValue); returns a value of type Color. It doesn’t modify the passed in parameter.
myColor.colorValue = EditorGUILayout.ColorField(“color”, myColor.colorValue);
1 Like
When using Unity’s default inspector the only tool you really have available is OnValidate() which, as you have pointed out, doesn’t give you any info on the diff of the objects state. If you wish to work with the default inspector your option is to either keep a record of the diff yourself, that is store the current state of the object in duplicate to check against, or to simply perform all operations dependent on the absolute state of the object inside every OnValidate (this isn’t as bad as it sounds and for most cases I’m all for being inefficient at editor time for the sake of simplicity.)
The second approach is similar to what you were exploring already, working with property setters/getters. For this however you will need to implement a custom editor to work with Unity’s inspector that will manipulate your object through the properties instead of Unity’s method of working directly with serialised fields.
Here are two simple example classes to illustrate this solution:
using UnityEngine;
public class ColorBehaviour : MonoBehaviour
{
[SerializeField] private Color m_Color = Color.white;
[SerializeField] private MeshRenderer m_Mesh;
public Color Color
{
get
{
return m_Color;
}
set
{
m_Color = value;
// React to our change in color here.
if (m_Mesh != null)
{
m_Mesh.sharedMaterial.color = m_Color;
}
}
}
public MeshRenderer Mesh
{
get
{
return m_Mesh;
}
set
{
m_Mesh = value;
// React to our change in mesh here.
if (m_Mesh != null)
{
m_Mesh.sharedMaterial.color = m_Color;
}
}
}
}
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ColorBehaviour))]
public class ColorBehaviourEditor : Editor
{
[SerializeField] private ColorBehaviour m_CustomBehaviour;
private void OnEnable()
{
m_CustomBehaviour = (ColorBehaviour)target;
}
public override void OnInspectorGUI()
{
m_CustomBehaviour.Mesh = (MeshRenderer)EditorGUILayout.ObjectField("Mesh Renderer", m_CustomBehaviour.Mesh, typeof(MeshRenderer), true);
m_CustomBehaviour.Color = EditorGUILayout.ColorField("Color", m_CustomBehaviour.Color);
}
}
I’ve also attached the full unity project if you want to see it setup and working.
1706840–107442–ForumHelp.zip (127 KB)
1 Like
THANK YOU!!!
I feel silly for not seeing the part about setting the variable when calling the layout function…
The getter and setter solution is perfect!!! 
What would you recommend for C# learning ?
Wow, that is a tough question.
I think it depends a lot on the sort of person you are as to what material you are going to absorb best and what method for learning you’re going to get the most out of.
I would say the safest answer, assuming your sphere of interest is based largely around Unity, is their relatively new and ongoing series of scripting tutorials which you can find here. You can hop into the intermediate section if you are already comfortable with some of the more basic concepts.
The greatest advantage with this series is not only does it teach you C# from the ground up, but it does so side by side with Unity itself, which is great for figuring out some of the nuances of C# in Unity vs regular development.
1 Like
I’m a concepts guy, for example, to remember how old I am I just plus 10 to the current year 
Started watching the series you mentioned.
Wish I would of spotted these tuts in April…
Had no idea they had scripting tuts; so thank very much for pointing these out.
And I had no idea that OnValidate existed!