Object Oriented Programming and Interactble Objects

Hi all-

I’m still relatively new to object oriented programming and while I’ve watched the Unity tutorials on the topic, I’m still having trouble wrapping my head around a problem I’m trying to solve.

I have a number of interactable objects in the world that can be clicked on. First, I shoot a ray from the camera when the player clicks, determine the collider that was hit, and then find the colliders game object. In order to determine if the object is interactable, I see if the game object that was found has a component of type interactable (a script with that name). If so, I know I’ve clicked on an object that can be interacted with. However, the subsequent code that runs thereafter could be very different depending on the type of object that was clicked.

Once I determine that the object that was clicked can be interacted with, how can I call into a specific function or look for a specific component for that interactable type when I don’t know exactly what it’s called?

Thanks in advance!

Unity introduced some new functionality in 4.6 that allows you to hook into the EventSystem. This can do all of the raycasting for you.

For selecting the method to call you normally have all of the components that can receive your event implement a specific interface. Then you can simply call methods from that interface, regardless of what the actual component is.