what is the best way in Unity to handle default values of a prefab?
When I declare public variables in my scripts, I usually do not initialise them with values. Instead, after clicking together a prefab (i.e. usually some piece of geometry with some scripts), I go to the inspector view of the prefab and change the values there.
But sometimes, I later want to change the public variables of the script, or even rename the script (and its class!) altogether. The initial values of the prefab are lost. I think this also applies to all existing instances of the (now changed!) prefab.
Initially, I thought it is best to initialise public variables in code so that this does not happen. Well, this certainly works. However, if you later type anything different in the prefab’s inspector, the initialisation in the code is overridden.
Also, when you create an instance of a prefab, in the inspector it takes the prefab’s initial values. When you change them, they get bold. When you change them back to the initial value, they remain bold. What exactly does the boldness represent?
I don’t think I quite grasped how initial values are supposed to be best handled in Unity , please comment.
Unity stores the public values as part of the prefab. The values you specify in the script are just initial values, which are only used if no prefab-values have been defined before. You can right-click on the component to “Reset” the components public variables to the script-defaults.
If a value appears bold in an instance, it means that you changed it from the original one, and that it is not connected to the prefab anymore. Means also if you change it back, it remains bold, because in case you change the prefab value of tha variable, the instance does not take it over anymore.
When a prefab is used in a scene an instance of it is created, the link is not broken because you created and instance or instantiated(unity prefab documentation) it from code or a script.
All private(not viewable within inspector)/public(viewable within unity’s inspector) will have the default value that either(as an instance in the inspector):
A) You gave them a default value in the code/script.
B) The variable type’s default(example: int defaults to 0, string is null, bool is false, etc).
Unity was awesome enough to recognize that you will want to create an instance(game object) and that you will want to change some values of the objects in that game object’s hierarchy without applying it to the prefab. The ability to pass those changes(defaults) back to the prefab exist in Unity or reset an instantianted prefab’s object back to default settings, the prefab is not de-linked just by changing a single value on a script from it’s default.
In the inspector you will see three options next to “Prefab” as below, which was taking from the documentation that i linked to.
Clicking apply will take the values from the instance of an object and apply it to the prefab, revert will take the values from the prefab and “reset” the instance of the prefab.