- Home /
NullReferenceException: Instatiate with construtor OR in Inspector
Hey Comm-unity,
i am currently working on a TowerDefense Game and want to implement an infinite level. I have a WaveSpawner with public HeaderVariables in which in can drag and drop Prefabs(Enemies) which will then be instatiated into the wave. These Header Variables are mostly arrays or Objects which include an Array and a number (To have different #Enemies for each run). For the infite level i added constructors to the these header variables to initialize all waves virtually and not in the inspector. This worked but however when i switch back to the normal levels I don´t get any Enemies. (I tracked the issue down to the constructor while using the Debugger but I could be wrong with that since i am very new to unity).
Maybe someone can smell what the issue is already. I am grateful for any help!
Thank you for your time. If can share my whole code if necessary.
Tim
NullReferenceException: Object reference not set to an instance of an object Wave.Startactually () (at Assets/Script/Wave.cs:24) WaveSpawner+c__Iterator0.MoveNext () (at Assets/Script/WaveSpawner.cs:116) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) WaveSpawner:Update() (at Assets/Script/WaveSpawner.cs:90)
Wave.cs
using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable] public class Wave{
public EnemiesPerWave[] enemies;
public bool shuffleBefore;
public int killNodes;
//private GameObject[] spawnList;
private List<GameObject> spawnList = new List<GameObject>();
private int enemyCounter = 0;
public void Startactually()
{
foreach(EnemiesPerWave entry in enemies){
for(int i = 0; i<=entry.count; i++)
{
spawnList.Add(entry.enemy);
}
}
}
public float rate;
public GameObject getNextEnemy()
{
GameObject ret = spawnList[enemyCounter];
enemyCounter++;
return ret;
}
public int count()
{
// number of total enemies
return spawnList.Count;
}
public Wave(float rate, bool shuffleBefore, int killNodes, EnemiesPerWave[] wave1, bool isInfinity)
{
if (isInfinity)
{
Debug.Log("const");
this.rate = rate;
this.shuffleBefore = shuffleBefore;
this.killNodes = killNodes;
//this.enemies = null;
this.enemies = wave1;
Debug.Log(enemies.Length);
this.Startactually();
}
else
{
this.Startactually();
}
} }
Hi Tim,
As you are getting a null-reference in Wave.cs line 24, please provide some of the code in Wave.cs.
Uploaded the whole file. Guess then it makes a little more sense.
Answer by IvovdMarel · Aug 06, 2018 at 02:21 AM
The issue here is that your enemies array is null. Unity would serialize the array in the inspector automatically, therefore making it not-null (but just have a length of 0).
You can either initialize the enemies array in code or use the Unity inspector
For the Infinity-$$anonymous$$ode i am doing it in the code and for "normal" levels I am doing it in the inspector. The Infinty $$anonymous$$ode works (as in the initialization and spawning are not infite yet) But also when the array in the inspector is not null (It is full with values) they don´t get spawned. The Debugger shows that there are values in these variables also.
Your answer
Follow this Question
Related Questions
How would I create a script that spawns objects more frequently as time goes on? 3 Answers
Error: Instantiated Enemies don't get hit 2 Answers
How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer
How do I control where enemies spawn? 1 Answer
Instantiated enemy looks at player 1 Answer