- Home /
Question by
SovietBear · Aug 14, 2015 at 05:02 PM ·
c#errormethod
'NullReferenceException: Object reference not set of an object' When calling a method.
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)
Comment
Best Answer
Answer by Slobdell · Aug 14, 2015 at 06:26 PM
It's telling you exactly what's wrong, on line 19 of SpawnHelperScript "type" is null. So you need to change something wherever that method is being called from because it's passing in a string for type that hasn't been initialized.
Your answer
Follow this Question
Related Questions
My Function is not showing up in the OnClick Menu 2017.2.0f 0 Answers
Multiple Cars not working 1 Answer
My function doesn't appears in the OnClick button script 5 Answers
Animation Error 0 Answers
Distribute terrain in zones 3 Answers