There is one thing, which has been the bane of my Unity experience lately and this has to do with the behaviour of public int / float values for classes derived from MonoBehaviour.
Lets assume i have a simple (saved) script:
public class Enemy: MonoBehaviour {
public int speed;
}
Let us further assume this Script is attached to alot of objects (e.g. one for each base enemy type i will be spawning in a game).
Most of the enemies will have the same speed, but some are slower / faster so the variable āneedsā to be editable in the inspector.
Now i suddenly remember that i wanted to give an default value, so i change the script to
public class Enemy: MonoBehaviour {
public int speed = 2;
}
As result nothing will happen, because all my enemies had already the value 0 filled in for speed in the inspector. This is also true, if i decide the variable doesnt need to be visible in the inspector:
public class Enemy: MonoBehaviour {
[HideInInspector] public int speed = 2;
}
The only way, which found around this is to rename / comment out the variable (at every place where it is used). After that one has to manually fill in the exception values.
This is also a problem, if i want to change the default value later for any reason.
I have spent serious amout of time looking for bugs where all i did was having a variable use āoldā values from the inspector.
In short:
Is there a good approach for handling the Situation of variable
-
Are editable in the inspector
-
In a class attched to alot of GameObjects
-
Needs a default value
-
But there are some special class-instance, which need other values
The best solution i can think of is, that unity override values, which were never touched in the inspector, i.e. remebers that the field in the inspector was filled as default value from a script.