- Home /
Ensure Child Components Present on GameObject in Editor
I have a script which you add to one GameObject (SwitchGO) but triggers a script behaviour (SBComponent) on an other GameObject (TargetGO). I'd like to enforce that you can't drag into target member field of the SwitchGO in the editor unless the game object has a SBComponent present.
I'm not going to be around to set up all the SwitchGOs myself and would like to avoid drag and drop errors.
This is sort of like the RequiresComponent attribute but without adding components to the possible TargetGO if it doesn't already have them.
Answer by hvilela · Oct 17, 2012 at 11:12 PM
Your "target" field at SwitchGO can be of the SBComponent type instead of GameObject. This way you have two advantages. First you'll only be able to drag objects that has that component. Second, you already have the reference to the script, so you don't need to call GetComponent to have a reference to the SBComponent.
Yes that works. Need to start trusting Unity more. Thanks.
Your answer
Follow this Question
Related Questions
Hint at Inspector variables that have tooltips 0 Answers
How to change a selection effect in editor 0 Answers
Editor Script only executes when clicking away. 0 Answers
Play Unity Editor from VSCode? 0 Answers