Avoid using GetComponent to check if RayCast hit is interactable
What I'm trying to do is create an "onInteract" event. The way I wanted to implement this in my 3D game is by using a raycast in front of the player and store its hit in a variable. However, since not every object is interactable (that is, not every object has an "IInteractable" interface attached), I think I have to use hit.getComponent() but I've read that's an expensive call to use in the update method. Is there any other way I can check whether the RaycastHit has the IInteractable interface?
Don't try to optimise if it does not cause any issue.
You could filter the objects using tags and/or layers.
Those people are wrong. GetComponent checks the list of components, and lists can have thousands of items!!! But how many component do you ever have? 5? 6? It's not expensive (especially not compared to the raycast!)