Hello Unity peeps!
Been a while since I’ve had a good question. So here is the context.
This is a game that creates waves of zombies for the player to kill. I have written a spawner code, that will generate zombies, at predetermined locations. The data for all the waves, levels, and wait times is compiled into a array, and utilized by means a loop that spawns the zombies, advances the data, while updating the wait time between spawns.
The problem is, I’m a brand new code-monkey, and I’ve never before used arrays or loops. I’m guessing that I’ve improperly compiled my array data. Below I’ll show my code, with narratives showing what the debugs returned, as well as the error codes:
var test : GameObject;
var zed1 : GameObject;
var zed2 : GameObject;
var zed3 : GameObject;
var zed4 : GameObject;
var zed5 : GameObject;
var spawn1 : Vector3 = Vector3(832, 2, 850);
var spawn2 : Vector3 = Vector3(832, 2, 854);
var spawn3 : Vector3 = Vector3(836, 2.2, 824);
var spawn4 : Vector3 = Vector3(840, 2.2, 823);
var spawn5 : Vector3 = Vector3(861, 2.3, 827);
var spawn6 : Vector3 = Vector3(856, 2.5, 825);
var currentlevel : int = 1;
var levels : Array;
var level1 : Array;
var lvl1wave1 : Array;
var lvl1wave2 : Array;
var L = 0; // number showing current level
var W = 0; // number showing current wave
var Z = 0; // number used by the for loop to avdance through the wave data.
var wave : float = 0;
var wavespeed : float = 1;
var waveinterval : float = 5;
function Start () {
// code ment to compile level data into an array for spawn loop
// Sintax = Gameobject, spawnlocation (Vector3 data), repeated until final number-
// - which indicates wait time before next wave.
lvl1wave1 = new Array (zed1, spawn1, 10);
lvl1wave2 = new Array (zed2, spawn2, 50);
level1 = new Array (lvl1wave1, lvl1wave2);
Debug.Log (level1.length); // Reports 2 as expected
levels = new Array (level1);
Debug.Log (levels.length); // Reports 2, unsure why
}
function spawn () {
Debug.Log (L); // Reports 0 as expected
Debug.Log (W); // Reports 0 as expected
Debug.Log (Z); // Reports 0 as expected
test = (levels[L][W][Z]); //Generates error Code: GameObject not found
for (Z = 0; Z < levels[L][W].length; Z += 2){
Instantiate (levels[L][W][Z], levels[L][W][Z + 1], transform.rotation);
if (Z == levels[L][W].length - 3){
waveinterval = levels[L][W][levels.length-1];
wave = 0;
if (W <= levels[L].length - 1) {W += 1;}
else {
if (L <= levels.length - 1) {L += 1;}
else {
//Endgame WIN Placeholder
Debug.Log("You Win");
}
}
}
}
}
function FixedUpdate () {
if (wave == waveinterval) {
spawn();
}
wave += Time.deltaTime * wavespeed;
wave = Mathf.Clamp(wave, 0, waveinterval);
}
If the test portion creating my error code is deleted, the for loop right after it generates an error as well. Namely: Object reference not set to an instance of an object. So I’m guessing that my array didn’t actually get the data i was trying to put inside it.
Any suggestions would be wonderful, I’m at a total loss myself >.<