- Home /
Is there a way for subclasses to inherit variable type and name but have their own values?
I would like to define variable type and name in the base class like this:
class Element extends Monobehaviour {
var z : int;
}
Then I would like classes which inherit from the base class to retain the variable type and name but assign their own value:
class Gold extends Element {
z = 79;
}
class Silver extends Element {
z = 47;
}
However, this assignment should only affect the instance of the variable within that class, not the base class or other subclasses:
// Element.js
// no value declared but defaults to 0 if attached to gameobject
Debug.Log("z");
// 0
// Gold.js
Debug.Log("z");
// 79
// Silver.js
Debug.Log("z");
// 47
Is there a way to do this in Unity (either through inheritance or other functionality)? I anticipate creating a large number of subclasses and would prefer to have standardized variable names and type declarations for simplicity and convenience without resorting to copy-pasting if possible. Thanks.
Your answer
Follow this Question
Related Questions
Create an array of classes, of different type. 1 Answer
Reading and Storing External Data into Memory (From Text) 1 Answer
Referencing subclass in Unity JS 2 Answers
if (!(apple is Vege)) then... 1 Answer
Creation of RPG Class Stat Presets? 1 Answer