- Home /
Calling MonoBehaviour's Reset() method
The MonoBehaviour
component offers by default a Reset()
callback, where we can customize the default values of our component's fields when clicking on the Reset option in the component's context menu.
I am interested in being able to call such callback through code, with the objective of better scaling the process of resetting multiple components. For example: I have components A, B, C in different GameObjects, and for testing's sake I intend to tweak their values outside of Play mode, and eventually after such tweaking I would like to reset such components without having to click n times on each of their context menu Reset option. One way of doing so, or so I thought, would be creating a component with an array of MonoBehaviours (all set through the inspector) and a context menu option that would call the Reset()
method of all the MonoBehaviours in such array.
I am aware that by default Unity's documentation states that the Reset()
of MonoBehaviour
method is made to be called through the inspector, but I was wondering if by any chance anyone is familiar with a non-oficial way of doing such.
Thanks in advance for any help!
No reason why not! After all, if it's in an editor script, it's kind of 'above' the rest of the functionality anyway.
To make it work, don't forget to make this method public:
public void Reset() {
// your code here
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613065343im_/https://answers.unity.com/themes/thub/images/avi.jpg)