• 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 /
This question was closed Dec 31, 2020 at 11:12 AM by Szkeptik for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Szkeptik · Dec 30, 2020 at 01:44 PM · loopinheritanceabstract

How to work with a list of base class, if a variable is only found in some subclasses?

Hi! I'm new and I'm working on a small strategy game where I'm making different units. All my units derive from the Troop Class. The specific units can be three types that are subclasses of Troop: Living, Demon and Undead. These have further Subclasses, which are the specific kinds of units, such as Goblin : Living.

My problem is, that some subclasses have their own variables, such as Living has foodUse, that the others don't have. When I try to loop through a List(Troop) looking for the amount of foodUse in each instance, it will give an error, that Troop doesn't have a variable for foodUse. This is understandable, as the variable only exists in the Living subclass. Is there a way for such a loop to only look at instances of Troop that happen to have foodUse, and disregard instances that don't? (While still using List(Troop) instead of List(Living) ?) Or maybe there's just a better way of doing this than the subclassing I'm doing? Thanks!

 public abstract class Troop : UnitBase
 {
     string[] _name;
     string[] _tags;
     int _type;
     int _hp;
     public DamageType damageType;
     public enum DamageType
     {
         Slashing,
         Piercing,
         Blunt,
         Magical,
     }
     
     public string[] name { get => _name; set => _name = value; }
     public string[] tags { get => _tags; set => _tags = value; }
     public int type { get => _type; set => _type = value; }
     public int hp { get => _hp; set => _hp = value; }
 }



   public abstract class Living : Troop
     {
         float healMultiplier = 1f;
         public int foodUse;
     }
     
     public abstract class Demon : Troop
     {
         float healMultiplier = -1f;
         public int apetite;
     }
     
     public abstract class Undead : Troop
     {
         float healMultiplier = -0.5f;
     }

The specific unit types then inherit from these:

 public class Goblin : Living
 {      
     public Goblin(Alligeance thisalligeance)
     {
         alligeance = thisalligeance;
         name = new string[2] { "Goblin", "Goblins" };
         hp = 8;
         damageType =  DamageType.Blunt;
 foodUse = 1;
     }
 }

In other class:

    foreach (Troop unit in troops) { food -= unit.foodUse; } //Throws error, not all units have foodUse.

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

  • Sort: 
avatar image
0
Best Answer

Answer by Roger_0123 · Dec 30, 2020 at 03:42 PM

Hi! You should upcast the Troop object to a Living one:

 Troop troop = //something
 if(troop.IsInstanceOfType(Living)){ //Check if upcasting if possible, otherwise you'll get a runtime error!
 Living living = (Living) troop; // Upcasting
 living.foodUse = //access property
 }

Check upcasting here

Hope this helps!

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
avatar image
0

Answer by sacredgeometry · Dec 30, 2020 at 06:23 PM

If you are using C# 7 you could use pattern matching:


 foreach(var unit in troop)
 {
     if (var unit is Living livingUnit)
     {
         livingUnit.foodUse // Use here
     }
 }

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

113 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 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

How should I structure my unit class relationships? 1 Answer

C# unity custom editor multiple different components but same base class 2 Answers

Abstract Class - Monobehaviour Functions 0 Answers

List of Inherited abstract Objects assigned via Inspector 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges