- Home /
Exposing Variable and Enforce Component Dependencies.
I want to expose a GameObject variable from a script. However, the GameObject passed into said script should contain a specific component.
Is there a way to enforce this at the Inspector interface or at compile time?
Answer by simonmc · Jun 01, 2012 at 02:50 AM
just make the exposed variable the type of the component that you want to enforce. That way, only gameobjects with that component attatched can be used. You can still access the gameobject the component is attatched to with the Component.gameObject
reference
In most situations this is the best approach. Finding components is always slower than getting the owning gameobject ;) $$anonymous$$ost the time you need only a specific component from another object. For example when you have a script that changes the material of another object when you enter it's trigger, you would make the target variable of type Renderer and not GameObject.
Yea, this seems to be what I was looking for. Thanks so much.
Didn't realize this was the hierarchy of the GameObject's and Components formed.