Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

General class for all script-classes?

Discussion in 'Editor & General Support' started by krillinat0r123, Feb 5, 2017.

  1. krillinat0r123

    krillinat0r123

    Joined:
    Feb 5, 2017
    Posts:
    5
    My problem is that in my game i have three different enemies, a skeleton, a skeleton mage and a scorpion. On each of those i have a common script called "EnemyBaseScript". This script checks by name which enemy it is and calls "Scorpion.function", "Skeleton.function" or "SkeletonMage.function" with a switch case.

    private void attackPlayer()
    {
    switch (Enemy_)
    {
    case 1:
    StartCoroutine(SkeletonNormal.attack());
    break;

    case 2:
    StartCoroutine(SkeletonMage.attack());
    break;

    case 3:
    StartCoroutine(Scorpion.attack());
    break;
    }
    }

    I think this makes bad performance, especially if i would like to expand my game with say 10 different enemies?
    - Is there a way to have general class for all the scripts so i can do something like this:

    private commonClass thisEnemy;

    if(this.name == "Skeleton")
    {
    thisEnemy = this.getComponent<SkeletonScript>();
    }
    else if(this.name == "Scorpion")
    {
    thisEnemy = this.getComponent<ScorpionScript>();
    }
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,985
  3. krillinat0r123

    krillinat0r123

    Joined:
    Feb 5, 2017
    Posts:
    5
    Thanks for the answer. I have seen the links but i still don't see how my problem can be solves using inheritance and/or interfacing? Can you give me a more detailed answer?
    - Thanks
     
  4. guzzo

    guzzo

    Joined:
    Feb 20, 2014
    Posts:
    79
    Code (csharp):
    1.  
    2. public abstract class BaseEnemy
    3. {
    4.     public abstract void Attack();
    5. }
    6.  
    7. public class Skeleton: BaseEnemy
    8. {
    9.     override public void Attack()
    10.     {
    11.         //Skeletonattack code
    12.     }
    13. }
    14.  
    15. public class Scorpion: BaseEnemy
    16. {
    17.     override public void Attack()
    18.     {
    19.         //Scorpionattack code
    20.     }
    21. }
    22.  
    Then you can do something like:

    Code (csharp):
    1.  
    2. BaseEnemy enemy;
    3.  
    4. gameObject.GetComponent<BaseEnemy>().Attack();
    5.  
    6.  
     
    Last edited: Feb 6, 2017
  5. krillinat0r123

    krillinat0r123

    Joined:
    Feb 5, 2017
    Posts:
    5
    Oh i see! - Thanks a lot Guzzo, really helped me!