- Home /
The question is answered, right answer was accepted
OnValidate() not called Unity 5.1.0f3
OnValidate() is only called upon script load or change from edit to play mode. Why? Not working in 5.3.5 neither. When i change values in my script, OnValidated should be called, but it's not.
Answer by SkandYxyz · Aug 11, 2016 at 07:17 PM
In my case the problem was, that I used a custom editor. When you use a custom editor, the OnValid() function is not called anymore automatically. Thus, you need to call it e.g., when a parameter changed or in the OnInspectorGUI() function, if it's no matter if it's called more often.
Answer by Holy_Crapf · Aug 09, 2016 at 06:36 PM
Just to be clear, the problem is that it's not being called during editor mode right? Because if the problem is that it's not being called in runtime, then that's because it's not supposed to be called then.
It's not called in editor mode.
But I just created a test monobehaviour script and there it works. (First, OnValidate() also seemed not to be called). In my script it still won't work.
And it's inheriting from $$anonymous$$onoBehaviour? Any chance you could show the script that the problem occurs in?
Thank you for your ideas! I am sorry, I don't want to post the code. But I realized, if I create a new script with the exact same content it works. $$anonymous$$aybe it has to do with script serialization? $$anonymous$$aybe I try to reimport everything.
No, that wasn't the problem. I guess I am near the problem. I have a CustomEditor class for this $$anonymous$$onoBehaviour and i guess I need to handle OnValidate() there.
Yes, it works when I remove the customer editor script.
I can post this piece of code - what do I need to do to call the OnValidate function? :
using UnityEngine;
using System.Collections;
using UnityEditor;
/// <summary>
/// Custom Editor for the Billboard Script
/// </summary>
[CustomEditor(typeof(Billboard))]
[CanEdit$$anonymous$$ultipleObjects]
public class BillboardCustomEditor : Editor
{
/// <summary>
/// Called to display custom GUI
/// </summary>
public override void OnInspectorGUI()
{
// The referenced Billboard script
Billboard _referenceBillboard = (Billboard)target;
// Display and retrieve billboard class as string
_referenceBillboard._billboardClass = EditorGUILayout.TextField("Billboard Class", _referenceBillboard._billboardClass);
// Display and retrieve target game object
_referenceBillboard._targetGameObject = EditorGUILayout.ObjectField("Target Game Object",_referenceBillboard._targetGameObject,typeof(GameObject),true) as GameObject;
// Display and retrieve size integer
_referenceBillboard._size = EditorGUILayout.IntField("Size",_referenceBillboard._size);
// Display and retrieve count integer
_referenceBillboard._count = EditorGUILayout.IntField("Count",_referenceBillboard._count);
// Display and retrieve y-offset float
_referenceBillboard._yOffset = EditorGUILayout.FloatField("Y Offset",_referenceBillboard._yOffset);
// Display and retrieve zoomfactor float
_referenceBillboard._zoomFactor = EditorGUILayout.FloatField("Zoom Factor",_referenceBillboard._zoomFactor);
// Check, wether to display dynamic lighting values or not.
if(_referenceBillboard._displayDynamicLightingValues)
{
SerializedProperty tps = serializedObject.FindProperty ("_dynamicLighting$$anonymous$$aterialSettings");
//EditorGUILayout.LabelField("Dynamic Lighting",EditorStyles.boldLabel);
EditorGUILayout.PropertyField(tps,true);
}
}
}
I can add this line and it will call the missing OnValidate() function, but to often - always on redraw.
// Call on validate function
_referenceBillboard.OnValidate();
Where can i put it to only call it when values change?