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...
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;
}
So unless you put in a link to the baseclass, it loses its connection as it were?
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
Follow this Question
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