This one is relatively straightforward.
For me it took a while before knowing the usefulness of the Reset() method call.
Initially I thought it would only apply when you click on the cog icon and press reset. But apparently it also executes once you add a component.
This is great if you want to setup references directly when adding components. Or if you want specific code once a component is added.
|public class ResetExample : MonoBehaviour|
|[SerializeField] private Rigidbody rigidBody;|
|[SerializeField] private SphereCollider sphereCollider;|
|private void Reset()|
|rigidBody = GetComponent<Rigidbody>();|
|sphereCollider = GetComponent<SphereCollider>();|
In case you are not familiar with some of the code above:
The RequireComponent attribute will require the gameObject to have a component.
If it doesn’t have the component it will add it automatically.
It will also make it impossible to remove any of the given components as long as the component
based on the script above is attached to the game object.
This means that the variable will show up in the editor, but it will still be protected from other classes by staying private.
It is good practice to use this attribute to reduce access to parts of the code you don’t want to be modifyable from other scripts.
- #if Unity_Editor
What you see here is a define symbol. You can read more about them here
Unity has multiple of these to specify if the code should be compiled or not,
depending on platform or other requirements.
In this case we don’t want this code to be included in the client code once it has been built.
Gets called once a component gets added. Or when you right click on the cog and select Reset.
Note that selecting reset will undo any changes,
The result after you add the component to the Game Object
Bonus tip: You can also add resources within the import settings.
Note that this only works for non-scene references. Such as prefabs, asset files and so forth.
All changes you make get stored within .meta files so be sure to include those if you ever decide to commit your changes or when you upload a awesome Unity plugin.