I’ve been working with Unity for a while, but I lack the vocabulary to look for answers to this specific question, so here is a wordy description of what it is:
Usually when we make a GameObject with a public field and edit the field, it affects the behaviour of the GameObject once we are in play mode. However, it does not create any immediate response in the editor itself, because it can only respond once the game scripts begin reading and interpreting it.
However, there exist in-build components which do create an immediate response in the game editor when we alter the values in them. For example, when we edit the Alpha value in the Canvas Group, its and its children’s opacity is affected immediately. When editing values in the Grid Layout Group, we see the child GameObject moved around immediately, in the engine window.
I am looking to create a prefab with a boolean value in one of its components, which, when changed, will change the Image component of one of its children, and display another child GameObject (whether through SetActive or through other means). All of this should happen within the Unity engine without having to start play mode.
Is this a technical possibility? If so, how do I achieve this result?
I believe you’re looking for the OnValidate method to do this.
You can add this method to any MonoBehavior or ScriptableObject, and it gets called automatically whenever anything in the inspector is changed. From there, you can change whatever other properties of whatever other objects that you want to.
There is also the Reset method you can add as well, which is called once when the script is created, and when the user right-clicks the script in the inspector & selects the “Reset” option.
You can use this method to initialize default properties of objects if you need to.
Note that OnValidate and Reset are editor-only methods, and are not included in a standalone build. Make sure not to include any runtime logic inside of them.