- Home /
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
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.
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?
$$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");
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
What about the inherited class constructor? I get errors and can't figure out how to use them.
Your answer
Follow this Question
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