# How to solve conflicting OnValidate() functions

I have two OnValidate() functions in two scripts, one attached to the Enemy (EnemyScript) game object and the other to the Enemies(EnemiesScript) game object.

In OnValidate() in EnemyScript I have the following:

    void PrepareEnemyType(EnemyType enType) {

// by default hasHibernated = false
if (!hasHibernated) {
if (enType == EnemyType.Green) {

}
else if (enType == EnemyType.Red) {

}
}

else if (hasHibernated) {

}
}


In OnValidate() in EnemiesScript I have the following:

for (int i = 0; i < enemies.Length; i++) {
if (!enemies [i]. hasHibernated) {
enemies[i]. hasHibernated = true;

}
}


based on the above all the enemies should change sprite to greyEnemy sprite, but that doesn't happen. Before I run the application all of them change to greyEnemy sprite initially, then after I run the application some of them change back to their original sprites, and when I stop running the application they sprites remain the same as when the application was running (different sprites). At this point, the only time that they all become grey again is when I make some other edit to either one of the scripts and save it. Then the above transition repeats. I suspect there could be some conflict with the two OnValidate() functions, not certain though. How can I resolve this?

• else if (hasHibernated) can be simplified to just else, the variable is a boolean so it only has two values and you already checked one of them. – Draco18s no longer trusts SE Jul 14 '17 at 15:32

OnValidate doesn't get called unless you manually set values in the inspector.