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.

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.