Does GameObject.GetComponent() find sub classes?

Go To


Working in the latest Unity3d 3.5 if it matters.

If I have a set of classes:

Class A extends MonoBehaviour
Class B extends Class A
Class C1 extends Class B
Class C2 extends Class B

And I have a GameObject that has C2 as a component. Will GetComponent.<A>() return the C2 component?

2012-04-03 21:32
by Chris


Well, I haven't tried a three-level hierarchy but, for two, I can confirm that GetComponent works as you expect.

2012-04-05 08:15
by Talemon


Also, this can be helpful: try to call


in case you need to get A just to call its method.

2014-11-12 14:57
by Vitalii Vasylenko