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
0
Question by Darkhalo45 · Feb 26, 2018 at 04:59 PM · inheritancesubclass

Inherited class doesn't show

I have a class called BuildingObject, and I need to make subclasses of the different types. When I make a subclass, the subclass doesn't even show up when I try to make a variable type. Another problem, is when my variable of type BuildingObject stores a subclass, how do I access the subclass variables?

Editor script:

 public class BuildingObject {
 
     [SerializeField]
     private GameObject _prefab;
 
     [SerializeField]
     private string _name;
 
     public BuildingObject(string name, GameObject go) {
         _name = name;
         _prefab = go;
     }
 
     public GameObject Prefab {
         get { return _prefab; }
         set { _prefab = value; }
     }
 
     public string Name {
         get { return _name; }
         set { _name = value; }
     }
 
 }

Type of building:

 public class MoneyGeneratorObject : BuildingObject {
 
     [SerializeField]
     private int _moneyPerSecond;
 
     [SerializeField]
     private int _maxMoneyHeld;
 
     public MoneyGeneratorObject(string name, GameObject go, int moneyps, int maxheld) : base(name, go, moneyps, maxheld) {
         _maxMoneyHeld = maxheld;
         _moneyPerSecond = moneyps;
     }
 
     public int MoneyPerSecond {
         get { return _moneyPerSecond; }
         set { _moneyPerSecond = value; }
     }
 
     public int MaxMoneyHeld {
         get { return _maxMoneyHeld; }
         set { _maxMoneyHeld = value; }
     }
 
 }

Editor script:

 private BuildingObject selectedObject;
 selectedObject.Name = "Random Name";
 selectedObject.MaxMoneyHeld = 1000; // This doesn't work
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
0
Best Answer

Answer by FuzzyLogic · Feb 27, 2018 at 02:01 AM

The subclass doesn't show up because the class contains an error. In your MoneyGeneratorObject() constructor, you are calling the base() constructor and passing 4 arguments but the constructor in the base class BuildingObject only accepts 2 arguments.

      public MoneyGeneratorObject(string name, GameObject go, int moneyps, int maxheld) : base(name, go) { // not, moneyps, maxheld) {
          _maxMoneyHeld = maxheld;
          _moneyPerSecond = moneyps;
      }

To access the fields within the subclass, you need to check if the object is derived from the appropriate type, then you need to cast the object to that type before you can read or write those fields (or call subclass methods).

       private BuildingObject selectedObject;
       selectedObject.Name = "Random Name";
       if (selectedObject is MoneyGeneratorObject) {
           ((MoneyGeneratorObject) selectedObject).MaxMoneyHeld = 1000;
       }

The above gets unwieldy when you start to have very many subclasses with divergent structures. For the moment, it should be sufficient but you should probably revisit this issue again if your code starts getting bloated with exceptional cases.

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 Darkhalo45 · Feb 27, 2018 at 03:36 PM 0
Share

I am using subclasses, because I need different variables for each different type of building I have and at the same time I want to keep a database of all those BuildingObject classes in one ScriptableObject List. Is there another way I could approach this?

avatar image FuzzyLogic Darkhalo45 · Feb 27, 2018 at 04:40 PM 0
Share

$$anonymous$$inimize access to the unique features of the subclassed building types. $$anonymous$$ake things generic. You can do stuff like having a public virtual void Initialize() function in the BuildingObject which you would override in the subclass as needed, to set default values and such.

BuildingObject:

 public virtual void Initialize(string name) {
     Name = name;
 }

$$anonymous$$oneyGeneratorObject:

 public override void Initialize(string name) {
     base.Initialize(name); // call the parent method in BuildingObject
     $$anonymous$$ax$$anonymous$$oneyHeld = 1000;
 }

Editor Script:

 private BuildingObject selectedObject;
 selectedObject.Initialize("Random Name");
avatar image
0

Answer by ShadyProductions · Feb 26, 2018 at 05:10 PM

BuildingObject is your baseclass which does not have MaxMoneyHeld property, but your inheritted class MoneyGeneratorObject does have MaxMoneyHeld property aswel as any property on your base class (Name.. etc).

  private MoneyGeneratorObject selectedObject;
  selectedObject.Name = "Random Name"; // This is accessable from the base class
  selectedObject.MaxMoneyHeld = 1000; // This will work because we are on the inheritted class
Comment
Add comment · Show 1 · 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 Darkhalo45 · Feb 26, 2018 at 05:27 PM 0
Share

What about the inherited class constructor? I get errors and can't figure out how to use them.

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

77 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

Related Questions

Using AddComponent to add a Sub Class using a String 4 Answers

Hide headers and variables in inspector from parent classes 1 Answer

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

inheritance ? (how to do it in javascript) 1 Answer

How to make subclass variables appear in inspector with enum 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