- Home /
GetComponent and Interfaces and warnings
So I have this IInteractable that I tag some of my monobehaviours with. The player has a trigger box attached to the front of them, and I have this little method running.
void OnTriggerEnter(Collider _col)
{
if (_col.GetComponent(typeof(IInteractable)))
{
interactObject = _col.GetComponent(typeof(IInteractable)) as IInteractable;
}
}
and then later in an update I call :
interactObject.Action()
to get the whole thing working.
Anyway, I'm having a problem, more of an annoyance really. Every time the trigger box touches anything in the game, I get this warning:
GetComponent requires that the requested component 'IInteractable' is inherited or implemented by 'objectName'.
And literally everything the trigger box touches gives this warning. The floor, the walls, the player themselves. This makes the console rather bothersome to use, even if I turn collapse on. Anyone have any ideas on how to make it not generate hundreds of warnings?
I will say, I found a solution that works, but requires a little more set up than I'd like. Just putting the box on the player on it's own layer, say PlayerInteract, and then every object that you can interact with in the game on it's own layer, Interactable. And then making it so PlayerInteract touches no layers except Interactable. This works, but I'm trying to see if I can just change the if statement to something else so I can avoid having to check layers all the time.
Basically, GetComponent won't work the way you want it to. It looks for, specifically, the type IInteractable- it won't detect a component that simply obeys the interface. Because IInteractable is an interface, not a type, GetComponent won't work with it.
@syclamoth you are mistaking. It will work as long as the object does inherits Component. I use it a lot in my projects.
Answer by Crump · May 11, 2012 at 05:56 AM
So it does work. It just throws a lot of warnings. Like a lot a lot. I'm just trying to see if I can get the warnings to go away. It expects that everything it touches to have implemented IInteractable in some shape, way or form. What's odd, to me at least, is that if it's not an interface, it doesn't throw that warning. Unity is totally cool with doing a GetComponent<> as a check, and failing without throwing a warning. Interfaces still work, but they seem to behave differently.
GetComponent<>
performs a check to see if the object does inherits Component.
On the other hand, GetComponent(typeof) does not. But your object still needs to inherits Component (or $$anonymous$$onoBehaviour) somehow.
This is indeed very useful for interfaces. You can ignore the warning safely: the method will return null if your object is not a component.
Answer by Pangamini · May 17, 2012 at 05:06 PM
I had the same problem. The GetComponent works well, but throws warnings. Here's my solution a custom GetComponent method:
def GetComponent[of T(class)](gameobj as GameObject) as T:
comps = gameobj.GetComponents(Component)
for comp in comps:
if comp as T != null:
return comp as T
return null
Your answer
Follow this Question
Related Questions
hotspots in 3d space 0 Answers
How can a game object detect that it is being hit by a raycast? 3 Answers
Getting only one collider form OnTriggerEnter2D or OnCollisionEnter2D. 0 Answers
Trigger with get key down problems 0 Answers
OnTriggerEnter being called event though there is no collision? 0 Answers