Whenever I call the coroutine spawnDelay found in this script:
public string spawnPos;
public GameObject DataKeeper;
public SpawnHelperScript SHS;
public bool canSpawn = false;
public int spawnDelayTime = 2;
public int testSpawn = 5;
GruntAI GAI;
// Use this for initialization
void Awake () {
DataKeeper = GameObject.FindGameObjectWithTag("DataKeeper");
SHS = DataKeeper.GetComponent<SpawnHelperScript>();
}
// Update is called once per frame
void Update () {
}
public void spawnGrunt(int amount){
Debug.Log ("spawnGrunt called!");
GameObject newEnemy = (GameObject)Instantiate (grunt, transform.position, transform.rotation);
GAI = newEnemy.GetComponent<GruntAI>();
if (!SHS.midSpawned && SHS.enemyCount == 0) {
SHS.enemyCount ++;
SHS.midSpawned = true;
GAI.Pos = "mid";
} else if (SHS.midSpawned && (SHS.rightCount + SHS.leftCount) <= 0) {
SHS.enemyCount ++;
SHS.rightCount ++;
GAI.Pos = "right";
} else if (SHS.leftCount > SHS.rightCount) {
SHS.enemyCount ++;
SHS.rightCount ++;
GAI.Pos = "right";
} else if (SHS.leftCount < SHS.rightCount) {
SHS.enemyCount ++;
SHS.leftCount ++;
GAI.Pos = "left";
} else if (SHS.rightCount == SHS.leftCount) {
int RandomNum = Mathf.RoundToInt(Random.value);
if(RandomNum == 0){
SHS.enemyCount ++;
SHS.leftCount ++;
GAI.Pos = "left";
}else if(RandomNum == 1){
SHS.enemyCount ++;
SHS.rightCount ++;
GAI.Pos = "right";
}
}
}
public IEnumerator spawnDelay(int D, int amount){
for (int i = 0; i < amount; i++) {
spawnGrunt(amount);
yield return new WaitForSeconds (D);
}
}
}
From this script:
using UnityEngine;
using System.Collections;
public class SpawnHelperScript : MonoBehaviour {
public int enemyCount;
public int leftCount;
public int rightCount;
public bool midSpawned = false;
public EnemySpawnScript ESC;
public GameObject ES;
void Awake () {
ES = GameObject.FindGameObjectWithTag ("EnemySpawner");
ESC = ES.GetComponent <EnemySpawnScript>();
Debug.Log ("Awake called");
}
public void wave(int enemyNum, string type, int SpawnDelay){
if (type == "grunt") {
StartCoroutine (ESC.spawnDelay(SpawnDelay, enemyNum));
} else {
Debug.LogError ("Wave Enemy type non-existant!");
}
}
}
Which is being called from this script:
using UnityEngine;
using System.Collections;
public class waveScript : MonoBehaviour {
public GameObject DataKeeper;
public SpawnHelperScript SHS;
// Use this for initialization
void Awake () {
DataKeeper = GameObject.FindGameObjectWithTag("DataKeeper");
SHS = DataKeeper.GetComponent<SpawnHelperScript>();
SHS.wave (5, "grunt");
}
}
It spits out this error:
NullReferenceException: Object reference not set of an object SpawnHelperScript.wave (Int32 enemyNum, System.String type) (at Assets/Scripts/Game Scripts/SpawnHelperScript.cs:19)