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 /
  • Help Room /
avatar image
0
Question by Gilead7 · Sep 13, 2016 at 07:11 PM · c#inheritance

Consumable Inheritence Issue

Setting up base classes for items, weapons, armor, consumables, etc. Ran into an issue where it claims "does not have a constructor for this class with zero arguments. See what is amiss please:

 Item Base Class:
 
 using UnityEngine;
 using System.Collections;
 
 public class Item
 {
     public int ID{get; set;}
     public string Name{get; set;}
     public string Description{get; set;}
     public int Value{get; set;}
     public int Weight{get; set;}
     public bool Stackable{get; set;}
     public string ModelPath{get; set;}
     public Sprite Icon{get; set;}
 
 }

Consumable: using UnityEngine; using System.Collections;

 public class Consumable : Item
 {
 
     public int Uses{get; set;}
 
     public Consumable(int Consumableid, string Consumablename, string Consumabledescription, int Consumablevalue, int Consumableweight, bool stackable, string modelPath, Sprite icon, int uses)
     {
         ID=Consumableid;
         Name=Consumablename;
         Description=Consumabledescription;
         Value=Consumablevalue;
         Weight=Consumableweight;
         Stackable=stackable;
         ModelPath=modelPath;
         Icon=icon;
 
         Uses=uses;
 
     }
 
 }

Missiles:

 using UnityEngine;
 using System.Collections;
 
 public class Missiles : Consumable 
 {
     string MissileType{get; set;}    //arrow, bolt, rock, etc.
     int MinDamage{get; set;}
     int MaxDamage{get; set;}
 
     public Missiles(int missileID, string missileName, string missileDescription, int missileValue, int missileWeight, bool stackable, string modelPath, Sprite icon, int missileUses, string missiletype, int mindamage, int maxdamage)
     {
         ID=missileID;
         Name=missileName;
         Description=missileDescription;
         Value=missileValue;
         Weight=missileWeight;
         Stackable=stackable;
         ModelPath=modelPath;
         Icon=icon;
         Uses=missileUses;
 
         MissileType=missiletype;
         MinDamage=mindamage;
         MaxDamage=maxdamage;
     }
 
 }

Thanks in advance...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ScaniX · Sep 13, 2016 at 08:04 PM

Your missiles class needs to explicitly call the base class constructor. That one should be the one setting all those members that are defined in the base class. Subclasses should only set the properties introduced by them or alter the values if necessary.

 public Missiles(int missileID, string missileName, string missileDescription, int missileValue, int missileWeight, bool stackable, string modelPath, Sprite icon, int missileUses, string missiletype, int mindamage, int maxdamage) : base(missileID, missileName, missileDescription, missileValue, missileWeight, stackable, modelPath, icon, missileUses) {
      MissileType=missiletype;
      MinDamage=mindamage;
      MaxDamage=maxdamage;
 }

Comment
Add comment · Show 3 · 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 Gilead7 · Sep 14, 2016 at 03:04 PM 0
Share

Thanks Scanix!

avatar image Gilead7 · Sep 14, 2016 at 04:05 PM 0
Share

So unless you put in a link to the baseclass, it loses its connection as it were?

avatar image ScaniX Gilead7 · Sep 14, 2016 at 04:26 PM 0
Share

No, but to create an instance of a subclass, you actually need to construct the baseclass first.

$$anonymous$$issiles is just a more specific/extended version of Consumable, so it needs to be a fully Consumable as well to provide that functionality.

Your $$anonymous$$issile instance is a hierarchical combination of all your classes: object -> Item -> Consumable -> $$anonymous$$issile

As your Item inexplicitly defines a default constructor (without parameters) by just defining no constuctor at all, your Consumable does not have to do the same as $$anonymous$$issile.

Please look up some basics on classes and interfaces in C#, it will surely be benefitial. :)

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

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

Question About Unity Class Hierarchy 3 Answers

Overriding Start(), is this just bad code? 1 Answer

Saving/loading inherited class scripts (C#) 1 Answer

Not sure how to make my classes interact in the way I intend them to 1 Answer

Inheritance, List and PropertyDrawer,CustomPropertyDrawer and Inheritance 0 Answers


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