NullRefrenceExeption
Hey guys. I found this script in a older tutorial from 2015 and I cannot fix it. It gives me "NullReferenceExeption: Object reference not set to an instance of an object Spawner.OnEnable () (at Assets/Scripts/Spawner.cs:14) "
and
"NullReferenceExeption: Object reference not set to an instance of an object Spawner.OnDisable () (at Assets/Scripts/Spawner.cs:14
at the beginning and end of the consol.
{ public GameObject objectToSpawn; public int numberOfEnemies; private float spawnRadius = 30; private Vector3 spawnPosition; private GameManager_EventMaster eventMasterScript;
void OnEnable()
{
SetInitialReferences();
eventMasterScript.myGeneralEvent += SpawnObject;
}
void OnDisable()
{
eventMasterScript.myGeneralEvent -= SpawnObject;
}
void SpawnObject()
{
for (int i = 0; i < numberOfEnemies; i++)
{
spawnPosition = transform.position + Random.insideUnitSphere * spawnRadius;
Instantiate (objectToSpawn,spawnPosition,Quaternion.identity);
}
}
void SetInitialReferences()
{
eventMasterScript = GameObject.Find ("GameManager").GetComponent<GameManager_EventMaster>();
}
}
And the GameManager_EventMaster: { public delegate void GeneralEvent (); public event GeneralEvent myGeneralEvent;
public void CallMyGeneralEvent()
{
if (myGeneralEvent != null)
{
myGeneralEvent ();
}
}
}
Tutorial: https://www.youtube.com/watch?v=e3OimELNyMc∈dex=12&list=PLwyZdDTyvucwjwqucleVQB7U12H2JPvg5
Add comment
put some debug prints in SetInitialReferences().
eg, does the Find() succeed ? if Find() succeeds, does GetComponent() succeed ?