I have created a class which is supposed to store 4 vairables. A second class is supposed to store a list of the previous class. I have attached the code I have written below.
However, When I run this I get the following error:
NullReferenceException: Object reference not set to an instance of an object
testScript+engagement.addSpawnObj (.spawnOBJ newSpawnOBJ) (at Assets/Scripts/testScript.cs:28)
testScript.CreateRandomEngagement () (at Assets/Scripts/testScript.cs:61)
testScript.Start () (at Assets/Scripts/testScript.cs:46)
Could anyone explain what I have done wrong?
Thanks
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class testScript : MonoBehaviour {
public class spawnOBJ {
public float cost;
public int sp;
public int eType;
public int num;
public spawnOBJ (int spawnPoint, int numToBeCreated, int enemyType, float spawnCost) {
sp = spawnPoint;
num = numToBeCreated;
eType = enemyType;
cost = spawnCost;
}
}
public class engagement {
public float totalCost;
public List<spawnOBJ> spawnOBJlist;
public void addSpawnObj(spawnOBJ newSpawnOBJ) {
spawnOBJlist.Add (newSpawnOBJ);
calcTotal ();
}
public void calcTotal() {
totalCost = 0;
for (int i = 0; i < spawnOBJlist.Count; i++) {
totalCost += spawnOBJlist*.cost;*
-
}* -
}* -
}*
-
//public GameObject spawner;*
-
// Use this for initialization*
-
void Start () {*
-
CreateRandomEngagement ();* -
}*
-
// Update is called once per frame*
-
void Update () {*
-
}*
-
void CreateRandomEngagement () {*
-
spawnOBJ blah = new spawnOBJ (9, 3, 5, 3.0f);* -
print ("hello1");* -
engagement engage = new engagement ();* -
print ("hello2");* -
engage.addSpawnObj (blah);* -
print (engage.totalCost);* -
}*
}