Question by
dave_mm0 · Mar 05, 2019 at 03:29 PM ·
public variablevalidation
public members not assigned OnValidate
Hey, I'm using OnValidate() to verify that some of my public variables are assigned, so that I get an error message to reminds me to assign them.
public Animator character1;
void OnValidate() {
Assert.IsNotNull(character1, "ERROR: character1 is not asssigned!");
}
This is working as expected, with one exception. When I launch my game, OnValidate gets called twice. One call is fine, there is no assertion, and the second Asserts, as if my character1 is not assigned. I checked in my scene, there is only one instance of this script, and the variable is assigned. When running the game, there are no errors, character1 is indeed assigned, and using it works fine. It is just very annoying to get a false error message in the console.
Does anyone know why this would be happening?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612192957im_/https://answers.unity.com/themes/thub/images/avi.jpg)