So I have a MonoBehaviour with a List<> as a public member. Whenever I hit the play button, the MonoBehaviour calls its destructor and is reinitialized. I have a function in the Awake() command that checks for if the List is null and initializes it to an empty list, but it seems the list is always set to null .
I have some ContextMenu commands that modify the list and return its state. I can use these commands after Play is pressed and they all work fine. There are currently no calls to any of these context commands in aside from the one call in Awake(). I’ve included some of the relevant code here.
`
public class DinoSpawner : MonoBehaviour {
public List<DinoWave> waveList;
static DinoSpawner instance = null;
public static DinoSpawner Instance { get
{
if (!(instance == null))
return instance;
instance = (DinoSpawner)UnityEngine.Object.FindObjectOfType(typeof(DinoSpawner));
if (instance == null)
Debug.LogError("Couldn't find an instance of DinoSpawner!");
return instance;
}}
~DinoSpawner()
{
Debug.Log("Destroyed spawner");
}
// Use this for initialization
void Awake () {
Debug.Log("Created spawner");
ResetWaveList();
instance = this;
}
// Update is called once per frame
void Update () {
timeUntilNextWave -= Time.deltaTime;
int counter = 0;
while (timeUntilNextWave < 0 && !IsFinished())
{
StartNextWave();
counter++;
if (counter > 20)
Debug.LogError("Infinite loop!", this);
}
for (int i = 0; i < currentWave; ++i)
{
DinoWave wave = (DinoWave)waveList*;*
if (wave != null)
wave.Update();
}
- }*
[ContextMenu(“GetNumWaves”)]
public void GetNumWaves()
{
if (waveList != null)
Debug.Log(“There are currently " + waveList.Count.ToString() + " waves.”);
else
Debug.Log(“Wave list not initialized!”);
}
[ContextMenu(“Add Wave”)]
public void AddWave()
{
ResetWaveList();
waveList.Add(new DinoWave());
}
[ContextMenu(“Reset Wave List”)]
public void ResetWaveList()
{
if (waveList == null)
{
Debug.Log(“Resetting wave list”);
waveList = new List();
}
}
[ContextMenu(“Clear Waves”)]
public void ClearWave()
{
Debug.Log(“Manually clearing wave list”);
waveList.Clear();
currentWave = 0;
}
`
I’m just getting started in Unity so there could be something simple that I missed, but I couldn’t find anything when I searched.