Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GetComponent of an inherited class

Discussion in 'Scripting' started by Watkey, Jun 6, 2020.

  1. Watkey

    Watkey

    Joined:
    Jan 11, 2020
    Posts:
    1
    I have a base class that is inherited by some other controllers:

    AiController

    Code (CSharp):
    1. public class AiController : MonoBehaviour
    2. {
    3. public string myTeam;
    4. // Lots of other stuff...
    5. }
    When an object is hit by a Physics2D.OverlapCircleAll to find nearby enemies, I want to be able to tell which Team this AI is on as I don't want friendly fire. I can't use Tag as I use tag to differentiate different unit types that are shared between teams.

    I have tried gameObject.GetComponent<AiController>() but it returns null as it will actually be RangedAiController or another that inherits from AI Controller.

    Is there a clean way to get the myTeam property value from these collisions without doing this:

    Code (CSharp):
    1.     void FindNearbyEnemies()
    2.     {
    3.        Collider2D[] collisions = Physics2D.OverlapCircleAll(transform.position, 10f);
    4.         List<GameObject> enemies = new List<GameObject>();
    5.         for (int i = 0; i < collisions.Length; i++)
    6.         {          
    7.             if (collisions[i].gameObject.GetComponent<RangedAIController>() != null)
    8.             {
    9.                 if (collisions[i].gameObject.GetComponent<RangedAIController>().myTeam == enemyTeam)
    10.                     enemies.Add(collisions[i].gameObject);
    11.             }
    12.             else if (collisions[i].gameObject.GetComponent<ConverterAiController>() != null)
    13.             {
    14.                 if (collisions[i].gameObject.GetComponent<ConverterAiController>().myTeam == enemyTeam)
    15.                     enemies.Add(collisions[i].gameObject);
    16.             }
    17.             else if (collisions[i].gameObject.GetComponent<Base>() != null)
    18.             {
    19.                 if (collisions[i].gameObject.GetComponent<Base>().myTeam == enemyTeam)
    20.                     enemies.Add(collisions[i].gameObject);
    21.             }
    22.         }
    23.         potentialTargets = enemies;
    24.     }
    Any help would be greatly appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    You can actually use interfaces for this purpose.

    Code (csharp):
    1. public interface ITeamBelonger
    2. {
    3.    string GetTeam();
    4. }
    Then any of your Monobehaviors can implement that ITeamBelonger interface.

    When you want to use it,

    Code (csharp):
    1. var teamBelonger = gameObject.GetComponent<ITeamBelonger>();
    And if it comes back non-null, you call the GetTeam() method.
     
    Munchy2007 and Ardenian like this.