I’m writing 2 classes as part of an inheritance hierarchy (Person → Adult), with Person being the superclass, and Adult being the concrete class.
I don’t want there to be any instances of Person, but obviously instances of Adult. Trying the keyword Abstract at the start of the Person call through out an error. Am I doing something totally wrong or is this a JS thing or a Unity thing? (If it is a JS/Unity thing, is there any work-arounds for this)?
Thanks.
Unity JS neither supports abstract class, interfaces nor extend + implement
For these things you would need to use C# (or Boo)
I’ve used the following code:
class Adult extends Person {
function Adult(age : int, sex : int, wealth : int, healthLevel : int, happinessLevel : int, foodLevel : int, restLevel : int) {
super(age, sex, wealth, healthLevel, happinessLevel, foodLevel, restLevel);
}
And the console has thrown up no errors. Does Unity JS not support inheritance at all then? I.e. I can’t use the extend keyword at all?
It does support inheritance, but only single inheritance (so one class extends another class).
You can not inherit from more than one class (and due to the lack of interfaces, you can’t extend from one class and implement additional interfaces for example)
Phew. You scared me for a second then (this is a university project lol). So it basically can’t support multiple inheritance, or interfaces.
class Person extends MonoBehaviour {
protected var age : int;
protected var sex : int; // Have it as an int, i.e. 0 = male 1 = female, or use a string i.e. "Male" and "Female" ?
protected var wealth : double = 0.0;
protected var healthLevel : int = 100;
protected var happinessLevel : int = 100;
protected var foodLevel : int = 100;
protected var restLevel : int = 100;
// Person constructor
function Person(age, sex, wealth, healthLevel, happinessLevel, foodLevel, restLevel) {
this.age = 100;
this.sex = 0;
this.wealth = 0.0;
this.healthLevel = 100;
this.happinessLevel = 100;
this.foodLevel = 100;
this.restLevel = 100;
}
If this is my superclass to the above subclass, and I’ve got this attached to a game object called Person, with a child object called Adult. Why are my values for my adult object all reading 0?
Additionally, if I take away the extends MonoBehaviour (even though I know it does this by default) I start to get errors? (Been stuck on this for HOURS). Any help would be most appreciated.
Sorry for double posting.
But I understand the MonoBehaviour error, I guess this is due to the class not being able to find transforms, as it no longer extends from MonoBehaviour by default but the Person class. Still unsure why all my variables for Adult still equal 0.
if you don’t make it extend monobehaviour but explicitely declare it as class (not having the class there at all would make the file itself the class and automatically extend monobehaviour), it will extend fromObject and you can no longer attach it to the GO at all.
As for “why does it read as 0”
Because monobehaviours are not meant to be used through new at all (you should get 1+ nice errors on that).
they work with Awake and start.
if you want to use new, you must not use monobehaviour but instead Object (or System.Object) and create it within another monobehaviour.
also why exactly do you have that constructor there?
make above values public instead of protected and the function to function Start () {
and you can just set the values in the editor
The basic outline was to have a parent class Person which had common constant variables, that all the people in my city/village shared. I want to be able to instantiate several adults, all altering there values differently from each other.
Will not a constructor (I.e. instancing an object) allow me to have several instantiate game objects which differ in variable values, or will they all share the same values? (I’m new to US, as you can probably tell I’ve came from Java).
Thanks, for the help.
on init they would have the same values (as they always have when they are init with default values). These values then can be set to whatever you want them to be.
During the course of the simulation, would different adult objects (and children objects) be able to have different values - in the sense that since they inherit there vars from Person. I’m having a hard time getting my head around it since it’s not traditionally object orientated.
I.e.
Adult 1 Adult 2 have the same variables at Start().
Adult 1 goes to work today Person.restLevel–; while Adult 2 stays at home Person.restLevel++; I’m guessing since they’re two different game objects they’ll have different restLevel values, even though they’re both inheriting them from the Person script?
Each instance of a mono behaviour is a distinct set of data, so yeah each can have its own data 
Its not that much “not traditional OO”, its just not built forcefully around the common inheritance mess when you work with it.
Unity has built its end user development around components which stand for distinct “attributes / capabilities” of a game object (real world entity) and handle its work and data.
Lets take a living organism: here you would have component for brain, heart, liver, the muscles, … all working on their own and with potential weak relation ships to other objects (components) of the system (body of the being)
Thats not much different from what you do in oldschool (“traditional”) OO too, just that components allow flexible work as nothing is hardcoded forcefully within classes. Its a enhanced combined result of different patterns used in OO dev for long.
So for each of my concrete classes (Adult, Child) how do I create/initialize their own values, instead of them all sharing the superclass values?
In Java obviously I’d just construct a new object, feeding in the parameters. But at the amount if I print out one of the values (or change it by ++ or --), both child and adult are reading the same values (i.e. there going up at the same rate). Therefore, how do I set there own values independent of the superclass variable that’s been passed down?
Thanks 