Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kthieu · Jun 05, 2014 at 08:35 AM · enemycomponenttypeenemyaisubclass

How to obtain Subclasses of a class/type?

Say, I have a class called EnemyUnit and multiple subclasses of it called EnemyArcher, EnemyInfantry, etc.

I'm creating an AI for movement in a game and I want to have an EnemyArcher stay one space away from a player unit. The problem is how to check if the current iteration of EnemyUnit in a foreach loop [EnemyUnit enemy in enemyUnitList] is an EnemyArcher? All enemy units have the tag "Enemy" if that helps, but I doubt it.

Can GetComponent work? and if so, how? Thanks.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by CodeElemental · Jun 05, 2014 at 09:03 AM

Assuming you have the class hierarchy something like this :

EnemyUnit (base class)

  • EnemyArcher

  • EnemyCaster

  • EnemyWarrior

Then you can do :

 List<EnemyUnit> enemies; 
 
 // Fill the list
 
 // Iterate the list
 for (int i = 0; i < enemies.Length ; i++)
 {
   if (enemies[i] is EnemyArcher) 
   {
     // Enemy is archer subclass
   } else if (enemies[i] is EnemyCaster)
   {
     // Enemy is caster subclass
   } 
 }
Comment
Add comment · Show 2 · 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
avatar image Kthieu · Jun 05, 2014 at 09:16 AM 0
Share

Never knew about the "is" keyword Thanks!

avatar image Bunny83 · Jun 05, 2014 at 09:28 AM 0
Share

The is operator is a fundamental operator in OOP ;) However it's usually easier to directly use the as operator since both perform the same check, but the as operator also cast the reference into that type, if possible:

 for (int i = 0; i < enemies.Length ; i++)
 {
     EnemyArcher archer = enemies[i] as EnemyArcher;
     if (archer != null)
     {
         // Enemy is archer subclass
         // use "archer" here
         continue;
     }
     
     EnemyCaster caster = enemies[i] as EnemyCaster;
     if (caster != null)
     {
         // Enemy is caster subclass
         // use "caster" here
         continue;
     }
 }

The as-cast does a type check and doesn't throw an exception if it fails but simply returns null. The advantage is that you have both in once: The type check and the cast which is usually necessary since you want to access something specific for this class.

avatar image
0

Answer by fafase · Jun 05, 2014 at 09:22 AM

Use polymorphism, way better and faster.

Your enemy class contains all basic info about enemy, sub classes just override the contents when needed.

 public abstract class Enemy:MonoBehaviour{
     public abstract void Move();
 }
 
 public class EnemyCaster:Enemy{
     public override void Move(){print("EnemyCaster");}
 }
 public class EnemyArcher:Enemy{
     public override void Move(){print("EnemyArcher");}
 }

now you can create a collection of enemy and place those into it.

 List<Enemy>list = new List<Enemy>();
 
 foreach(Enemy enemy in list){
     enemy.Move();
 }

This will print the implementation in the sub class even though you have a collection of top class. This is polymorphism, one of the three big lines of OOP (encapsulation, inheritance, polymorphism).

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

22 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

Related Questions

Simple AI Error 8025 Parrsing error. 2 Answers

Enemy VISION range? Help! 0 Answers

How to make sub class be recognized as the same type as main class? 0 Answers

How to make if the object == null then add gold? 1 Answer

Moving character from point to point 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