- Home /
Retrieve a GameObject that has a component that is a subclass of a particular class?
For example, I have a base class for a component called Ship
, and a subclass called BigShip
.
My current situation is I have a player that has a void OnTriggerEnter (Collider other)
entering a ship. This player needs to have access to the ship component that exists in other
.
The best way I can think of getting the component is to iterate over all of the components and (somehow) individually check if each Component is a subclass of Ship
.
What's the best way of doing this?
Answer by gak0 · Jun 18, 2013 at 05:15 AM
After some trial and error, it turned out to be easy. Basically call GetComponent
on the base class. It seems to match anything that is subclassed from that Type automatically.
Yes, that's it. You can also find interfaces I believe with the non-generic version of GetComponent
Your answer
![](https://koobas.hobune.stream/wayback/20220613104401im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Creating an array of custom objects 1 Answer
Acces a component on an instantiated object 1 Answer
Get component in iPhone 1 Answer
Difficulties accessing other components 4 Answers