- Home /
why after assigning some field with "onvalidate" i need to reassign it after some time
In scene "1" onvalidate i assign some fields of some prefabs. private void OnValidate() { Island a = Game.Instance.IslandWithPrefabValues; magicswine = a.magicswine; }
p.s. except for that magicswine there also a lot of other fields
Then i check that all the fields that have to be filled with values are filled
Then i comment onvalidate (and save code) and play scene "menu" (in menu there are no Game so i have to comment it) for few times its ok all the fields like they have to be But suddenly i can get "UnassignedReferenceException: The variable magicswine of Island_Sharing_Parts has not been assigned. You probably need to assign the magicswine variable of the Island_Sharing_Parts script in the inspector."
Answer by idbrii · Mar 26, 2019 at 06:42 PM
I think it's because Unity doesn't know that your object was modified and didn't save your changes. You need to mark it as dirty. The docs recommend using Undo to mark dirty.
Try this:
#if UNITY_EDITOR
private void OnValidate() {
Island a = Game.Instance.IslandWithPrefabValues;
UnityEditor.Undo.RecordObject(this, "Set magicswine ");
magicswine = a.magicswine;
}
#endif