- Home /
GetComponents Access to Inherited Class
Hi all,
I know that using GetComponent() will return component of type B if B inherits from A. Does the same hold true for GetComponents() ? I can't get it to work.
I would expect this:
Reaction[] reactions = shootHit.collider.GetComponents<Reaction> ();
To find the following component:
public class ReactionDamage : Reaction {
public override void React();
}
GetComponents will return an array of all ReactionDamage components. Example by docs:
public HingeJoint[] hingeJoints;
void Example() {
hingeJoints = GetComponents<HingeJoint>();
foreach (HingeJoint joint in hingeJoints) {
joint.useSpring = false;
}
}
one more thing, I am not sure if you should do GetComponents on the shootHit.collider though, and can't know for sure unless you provide a little bit more code.
What sort of error do you get? Are you sure the Reaction components are on the same GameObject as the collider? That code should work, but the return type will be of Reaction, not ReactionDamage, so you won't be able to access anything specific to ReactionDamage unless you cast.
I wanted to access a React() method on my ReactionDamage component That method is defined as a public virtual in the Reaction class. So as you say, it works, I just had forgotten the override
keyword in the React() method defined in ReactionDamage. However all your comments and anwers are useful, as according to your answer if I hadn't created the method in the Reaction class, I would not have been able to call it since the returned type is of Reaction, not ReactionDamage. Did I understand you correctly ?
Answer by Bunny83 · Jun 12, 2017 at 06:12 PM
I created a test case with the following three classes:
//TestComponentBase.cs
public class TestComponentBase : MonoBehaviour
{
public virtual void TestMethod()
{
Debug.Log("Base");
}
}
//TestComponentDerived.cs
public class TestComponentDerived : TestComponentBase
{
public override void TestMethod()
{
Debug.Log("Derived");
}
}
//GetComponentsDerived.cs
public class GetComponentsDerived : MonoBehaviour
{
void Start ()
{
var array = GetComponents<TestComponentBase>();
Debug.Log(array.GetType().Name);
foreach(var c in array)
{
c.TestMethod();
}
}
}
I attached one "GetComponentsDerived", two "TestComponentDerived" and one "TestComponentBase" class to the same gameobject. Here are the results:
As you can see the return type is TestComponentBase[]
according to visual studio. In the console we can read the following:
TestComponentBase[]
Derived
Derived
Base
The first line is the actual type of the returned array as it is determined at runtime (printed by Debug.Log(array.GetType().Name);
). Also as you can see it correctly returned all 3 instances and correctly executed the right method.
The GetComponents method of a component (doesn't matter if you use Collider, a MonoBehaviour or any other Component) just calls the GameObject version. GetComponents<T>
is defined like this:
//UnityEngine.Component (UnityEngine.dll)
public T[] GetComponents<T>()
{
return this.gameObject.GetComponents<T>();
}
If it doesn't work for you, there is something wrong on your side. Either you work on the wrong gameobject, or none of the components you're looking for are attached to the object you're checking. Keep in mind that GetComponents only returns components attached to the exact gameobject. It won't return components attached to childs or parents.
Your answer
![](https://koobas.hobune.stream/wayback/20220612123905im_/https://answers.unity.com/themes/thub/images/avi.jpg)