- Home /
Parent class in getcomponent
Hello, for logical operations I have class called Brain and for each individual puzzle i have individual child classes of brain for example public class Puzzle 1: Brain {}
now I also have trigger objects, and trigger objects invoke function called "StartBrain" that is part of parent class on a game logic object. Because I don't know what will be the name of the script that is attached to logic object (Puzzle 1 or Puzzle 2), I use target.sendmessage("StartBrain", Parameters) to invoke this function on an object.
Now I was wondering if it is possible to use polymorphism with something like this. target.getcomponent().MyFunction();
and target has child class attached to it, so that I can call a child class as a parent class, and invoke the method in parent class.
I know unity uses something similar with colliders, where parent Collider class can be used for collision detection, regardless of whether object has child classes like boxcollider or sphere collider. But I don't have strong enough grasp of polymorphism theory to figure out how to do it myself.
If anyone can help me with this, it would be awesome
Basically my problem is that I want to write a function call that does not rely on you knowing the name of the child class.
Answer by shadesbelow · Sep 26, 2014 at 10:10 PM
Using events would be an efficient solution.
Another Events and Delegates reference (with yet other references):
http://answers.unity3d.com/questions/600416/how-do-delegates-and-events-work.html?sort=oldest
Your answer
![](https://koobas.hobune.stream/wayback/20220613141138im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I access an inherited method from a separate (collided) object? 1 Answer
How to GetComponent that derived from a certain base class? 2 Answers
Access GameObjects Parents and Childs from any level of hierarchy 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer
What's the difference between SendMessage and GetComponent? 3 Answers