List Data not persisting after being added in Start()
Hello all, I appologize, as this is likely a dumb question, but I am not used to using generic lists. I am trying to make a script that will control all of the enemy spawners in my scene. I need to hold the spawners in a genaric list, so that I can randomly select several of them(using the shuffle technique) and spawn enemies at them. the problem is that after finding and adding all the spawners and upon exiting the Start() function the list erases it self and is empty. I have read in several places that I need to serialize the list, but None have explained how to do this. My code is as follows
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class VRZ_SpawnController : MonoBehaviour {
private List<GameObject> SpawnList;
private int wave;
private int spawners;
private int totalEnemies;
private int liveEnemies;
private int groupSize;
public float initialStartDelay;
public int waves;
public int maxSpawnsPerWave;
public int maxSpawns;
public int maxGroupSize;
public float maxSpawnDelay;
public bool allowPrioritySpawn;
// Use this for initialization
void Start () {
spawners = transform.childCount;
List<GameObject> SpawnList = new List<GameObject>();
print(SpawnList.Count + "@1");
for (int i = 0; i < spawners; i++) //finds all the child spawners and adds them to the list, this part works fine
{
SpawnList.Add(transform.FindChild("Spawn (" + i + ")").gameObject);
}
print(SpawnList.Count + "@2");
StartCoroutine("Controller", initialStartDelay); //begins the spawning coroutine
}
int ZCount() // This counts total enemies alive, and is also working fine
{
print(SpawnList.Count + "@4");
int go = 0;
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("Enemy");
go = gos.Length;
liveEnemies = go;
return go;
}
private IEnumerator Controller()
{
while(true)
{
print(SpawnList.Count + "@3");
ZCount();
if (liveEnemies < maxSpawns && totalEnemies < maxSpawnsPerWave) // checks is too many enemies are spawned
{
print(SpawnList.Count + "@5");
int activeSpawners = Random.Range(0, SpawnList.Count);
List<GameObject> Temp = new List<GameObject>();
Temp = SpawnList;
for (int i = 0; i < activeSpawners; i++)
{
groupSize = Random.Range(1, maxGroupSize);
int pos = Random.Range(0, Temp.Count);
Temp[pos].GetComponent<VRZ_Spawn>().Spawn(groupSize);
Temp.RemoveAt(pos);
}
}
yield return new WaitForSeconds(10);
}
}
}
Only the print(SpawnList.Count + "@2"); shows that there is anything in the list, but all print statements are reached. I just can't seem to figure out why the list clears itself after the start function
Thanks in advance for the help!
Answer by qtoompuu · Oct 25, 2016 at 05:26 AM
Ok I solved it, for whatever reason adding the spawns using a function called from within start rather than directly doin it in start worked...
I'm guessing that your issue was caused by scope and name hiding. You declared the list once in your class and again with the same name in the start() method. if you changed
List<GameObject> SpawnList = new List<GameObject>();
to
SpawnList = new List<GameObject>();
in the start() method, it would probably work.
I think that you were creating a new "SpawnList" variable that was local to the start() method. You filled that local list in start(), so your print statement worked there, but as soon as the start method finished, that list went out of scope and was lost. The class-scoped List was then used in the rest of your methods, and it was never filled with items. I'm surprised you didn't get null reference errors.
Your answer
![](https://koobas.hobune.stream/wayback/20220612095356im_/https://answers.unity.com/themes/thub/images/avi.jpg)