- Home /
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!
Answer by Kiwasi · Jan 28, 2015 at 09:49 AM
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.
Thank you for your answer. I'll definitely look into interfaces more , as they appear to be what I'm looking for. I'll report back after testing to verify that they help solve the problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613181747im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Question on C# Interface Method Parameters 3 Answers
How to enable/disable image effect through C# in Unity 5 1 Answer
How to impress Unity Developer Recruiter 0 Answers