So I’m working on a script to spawn different types of buildings randomly. It’s working except I need more information to be stored for use later in an array of a class. Here’s the script.
var NumberOfSpawners = 25;
var MaxPolice = 1;
var MaxHouses = 6;
var MaxPawns = 2;
public class gridnumber {
var buildingtype : int;
var isfortified : boolean;
}
private var grid : gridnumber[];
private var spawnerid;
private var spawner : GameObject;
private var curPolice = 0;
private var curHouses = 0;
private var curPawns = 0;
function Awake () {
spawnerid = new int[NumberOfSpawners+1];
grid = new gridnumber[NumberOfSpawners];
}
function BuildingSelect (gridnum) {
var buildingtype : int;
// Decide what to spawn
buildingtype = Mathf.Round(Random.Range(1,11));
// Check if building type is allowed and filter as neccessary
if (buildingtype > 8) {
if (curHouses < MaxHouses)
buildingtype = 1;
else if (curPawns < MaxPawns)
buildingtype = 6;
else if (curPolice < MaxPolice)
buildingtype = 8;
else
buildingtype = 9;
}
if (buildingtype == 8 && curPolice >= MaxPolice) {
if (curHouses < MaxHouses)
buildingtype = 1;
else if (curPawns < MaxPawns)
buildingtype = 6;
else
buildingtype = 9;
}
if (buildingtype == 6 || buildingtype == 7 && curPawns >= MaxPawns) {
if (curHouses < MaxHouses)
buildingtype = 1;
else if (curPolice < MaxPolice)
buildingtype = 8;
else
buildingtype = 9;
}
if (buildingtype > 0 && buildingtype < 6 && curHouses >= MaxHouses) {
if (curPawns < MaxPawns)
buildingtype = 6;
else if (curPolice < MaxPolice)
buildingtype = 8;
else
buildingtype = 9;
}
// initialize grid class
**//grid[gridnum].buildingtype = buildingtype;
//grid[gridnum].isfortified = false;**
// Count
if (buildingtype > 0 && buildingtype < 6)
curHouses++;
else if (buildingtype == 6 || buildingtype == 7)
curPawns++;
else if (buildingtype == 8)
curPolice++;
return buildingtype;
}
The problem comes when I uncomment the 2 bolded lines. The exact error is “object reference not set to a reference of an object” I don’t see why I’m getting this error as all that should be happening is the number that was generated should be assigned to the buildingtype variable of whatever number of the array gridnum is.
Can anyone see what I’m doing wrong here?