Hi,
Could someone please explain how to make and manipulate Superclasses and Subclasses in Unity Javascript?
What I want to do is:
I have a game that has multiple waves of enemies per level. When all the enemies of Wave1 are killed, the game switches to Wave2 and changes many parameters.
I started by making two classes, Wave1Values and Wave2Values. Then I have a variable waveIs that should change from Wave1Values to Wave2Values when the WaveSwitch() happens. But of course, it can’t be changed because that would mean it changes it’s class which is not possible. Example:
class Wave1Values{ // this shows conveniently in the inspector
var maxEnemies = 4;
var minTime : float = 1.0;
var maxTime : float = 5.0;
var frequency : float = 1;
var defaultMinTime : float = 2;
}
var wave1 : Wave1Values = Wave1Values();
class Wave1Values{ // this also shows in the inspector ![]()
var maxEnemies = 8;
var minTime : float = 3.0;
var maxTime : float = 6.5;
var frequency : float = 2;
var defaultMinTime : float = 3;
}
var wave1 : Wave1Values = Wave1Values();
// then I have Start(), Update() and all sorts of stuff
//and somewhere during the game this function gets called:
function SwitchWaves() {
var waveIs : Wave1Values = wave1;
switch (waveNo){
case (1):
waveIs = wave1;
break;
case (2):
waveIs = wave2;
break;
}
//I actually tried to do this by making an array of WaveValues
//and getting them directly by number
//and not by switch, but it was problematic.
//like an array of [wave1, wave2, wave3 ... etc]
//and then get values by waveIs = array[waveNo];
//would that be possible if they are all different subclasses of one superclass?
maxEnemies = waveIs.maxEnemies;
minTime = waveIs.minTime;
maxTime = waveIs.maxTime;
frequency = waveIs.frequency;
defaultMinTime = waveIs.defaultMinTime;
}
But as I said, this won’t work since waveIs is already of Wave1Values class ![]()
So I thought of making a superclass WaveValues and make Wave1Values and Wave2Values it’s subclasses. But I don’t know how to do it despite searching the web and finding this: How to reassign a variable's class - Questions & Answers - Unity Discussions
That answer looks nice but it won’t work when I put it in my code. I get some errors about same names, ambiguous stuff etc. I tried a lot of variations. The only thing that I haven’t tried is to put the code with subclasses and superclass in another script that isn’t attached to any object in the game but that would be inconvenient because it wouldn’t show in the inspector. It would greatly help if someone could solve this so anyone who wants superclassing in Unity Javascript has a working example available at Unity Answers.
Thanx.