Detect changes in the value of other objects referenced?
I'm trying to implement a feature that detects a change in the value of another field object reference.
For example, there is an A object, and the B object contains A. The A object is notified by OnValidate when the field of the A object has changed. At this time. Is there any way that "B" can detect the change of "A"?
This feature is only required in the Editor and does not include code in Runtime.
public class A : ScriptableObject
{
[SerializeField]
public string team;
private void OnValidate()
{
Debug.Log($"My team name has been changed to {team}.");
}
}
public class B : ScriptableObject
{
[SerializeField]
public A a;
private void OnValidate()
{
Debug.Log($"A changed.");
}
// How can I detect changes to A and call this functions?
private void OnATeamChanged()
{
Debug.Log($"A's team name has been changed to {a.team}.");
}
private void OnADestroyed()
{
Debug.Log($"A is missing.");
}
}
Answer by SpasticDad · Dec 28, 2018 at 04:33 PM
You can call [ExecuteInEditmode] just above your class declaration like this:
using UnityEngine;
[ExecuteInEditMode]
public class YourClass : MonoBehaviour
{
which will make the code execute whist editing. That's one problem out of the way.
for the executing the other functions, try making them public, and adding the values that you want to display. In this case you also need to link the script that you want to call the functions on. Kind of like this:
public class A : MonoBehaviour
{
private B b;
private string teamName;
private void Onvalidate()
{
b.Changed(teamName);
}
}
public class B : MonoBehaviour
{
public void Changed(string TeamName)
{
print("Team name:" + TeamName);
}
}
Of course this al depends on where you are storing the scripts, so that you have to call them from those objects
It is reversed. A does not contains B and notify B of the change. B contains A and detects a change in A.
And this feature should also work with ScriptableObject.
Your answer
Follow this Question
Related Questions
Can't build my Project 1 Answer
Attach a ScriptableObject Asset to a GameObject in code 0 Answers
Referenced Bool is not updating 1 Answer
SetDirty won't work on Nested List 1 Answer
Unity and external plugins 0 Answers