Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Sorade · Jun 10, 2017 at 01:59 PM · inheritancegetcomponents

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();
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FunIsDangerous · Jun 10, 2017 at 03:05 PM 0
Share

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.

avatar image sleepandpancakes · Jun 10, 2017 at 04:02 PM 0
Share

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.

avatar image Sorade sleepandpancakes · Jun 12, 2017 at 10:37 AM 0
Share

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 ?

avatar image sleepandpancakes Sorade · Jun 12, 2017 at 04:30 PM 0
Share

yep that's right

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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:

ReturnType

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.


getcomponentsreturntype.png (8.6 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Instantiating an object from its interface 3 Answers

Is it possible to set variables for classes? 2 Answers

inheritance from a ScriptableObject 2 Answers

How to change variables from another script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges