- Home /
why does OnValidate() get called multiple times for a single update?
So, OnValidate() is getting called multiple times(5 actually) every time I tick a boolean in the inspector, and this object is a prefab. I have one prefab with my component on it, and one instance of the prefab in the scene. The first 4 calls are from the prefab, and the last one is from the scene instance. In OnValidate I attempt to get the PrefabStage, but it only grabs a reference to the stage properly on the first callback of the 4 that the prefab makes. Then CheckIfIsPrefab() evaluates to false on the three subsequent callbacks, HOWEVER, it never seems to update IsPrefab in the three subsequent calls. And then it does the instance OnValidate() callback for some reason, but that one behaves as expected and fails to grab the PrefabStage as it is an instance. This is the code:
private void OnValidate()
{
Debug.Log("Validating..."+this);
if (CheckIfIsPrefab() != true)
{
SerializedObject serializedObject = new SerializedObject(this);
SerializedProperty serializedPropertyGUID = serializedObject.FindProperty("guid");
PrefabUtility.RevertPropertyOverride(serializedPropertyGUID, InteractionMode.AutomatedAction);
}
}
private bool CheckIfIsPrefab()
{
PrefabStage prefabStage = PrefabStageUtility.GetPrefabStage(gameObject);
Debug.Log("IsPrefabSource: " + gameObject + " : " + prefabStage+", "+ (prefabStage != null));
return IsPrefab = (prefabStage != null);
}
So I thought it might be something with the new PrefabStageUtility.GetPrefabStage()
function, but even commenting everything out to simply
private void OnValidate()
{
Debug.Log("Validating..."+this);
}
It is still being called multiple times by ticking the bool in the inspector. 4 times by the prefab, and then once by the instance. Does anyone know why this is happening, and/or why on the three subsequent callbacks to OnValidate() on the prefab the references are failing to return and evaluate properly?
Edit: By the way, it only fires once, as expected on the prefab instance in the scene heirarchy when modifying it.
Edit 2: Further testing reveals that this likely has something to do with the prefab autosave feature. When unchecking autosave it work as expected calling OnValidate() once, however once I click save I again see three callbacks to OnValidate() for the prefab invoke where the PrefabStage fails to evaluate correctly followed by one more OnValidate() callback for the instance. Is this a bug with the new nested prefabs system? Are the extra callback benign? Or is there a case where they could incorrectly modify the prefab data?
Your answer
![](https://koobas.hobune.stream/wayback/20220612192102im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to play mecanim animations in Editor mode? 2 Answers
Working with Unity Editor 1 Answer
Editor Scripting - Add Component if certain Tag is selected 0 Answers