- Home /
How to create and display a list of enemies in upcoming wave?
Hi All,
I am working on a tower defense game, I wanted a way for the player to see the enemy units in the next wave,=. So my approach was to create a gui text object, add a refference to the waveManager(I just have it assigned in the inspector for now) class which holds all of the information for each wave. The waveManager class is broken down as a collection of events, and each even is broken down as a spawn events, which is where I set the enemy types and spawn locations. The spawn info is also a collection, so I have a list within a list. I get an argument out of range error, I know that I need to iterate over both the event and spawn infos collection, but I can't figure out how to do it correctly. Are there any examples on how to display the content in a wave?
public class WaveManager : MonoBehaviour {
public List<WaveEvent> events = new List<WaveEvent>();
public List <Text> nextWaveText; //List of enemies
private bool isPlaying = false;
public float waveDelay = 5f; //Used to delay the stArt of eacach wave
public void StartWave()
{
isPlaying = true;
if (events.Count != 0)
{
events[0].StartEvent();
for (int i = 0; i < events.Count; i++)
{
Debug.Log(events[i].spawnInfos[0]);
// nextWaveText[i].text = events[0].spawnInfos[i].spawnedEnemyName;
}
}
else
{
LevelManager.Instance.Endwave();
}
}
private void Update()
{
if (!isPlaying)
return;
if(!events[0].RunEvent())
{
Debug.Log("End Event");
events.RemoveAt(0);
if (events.Count == 0)
{
LevelManager.Instance.Endwave();
}
else
events[0].StartEvent();
}
}
}
//classs that controls what happens in a wave object
[System.Serializable]
public class WaveEvent
{
public float duration = 15.0f;
public List<SpawnInfo> spawnInfos;
// public List<GameObject> spawnpoints;
private float startTime;
public void StartEvent()
{
startTime = Time.time;
}
public bool RunEvent()
{
//No duration, and nothing to spawn and i empty, end the event
if (duration == 0.0f && spawnInfos.Count == 0)
return false;
else if(Time.time - startTime > duration && duration != 0.0f)
{
return false;
}
for(int i =0; i < spawnInfos.Count; i++)
{
spawnInfos[i].ReadyToSpawn();
if (spawnInfos[i].amount== 0)
{
spawnInfos.RemoveAt(i);
}
}
return true;
}
[System.Serializable]
public class SpawnInfo{
public int spawnPointIndex = 0; //The point at which new enemies are created
public int spawnPrefabIndex = 0; //The type of enemy to spawn
public string spawnedEnemyName;
//First two called from spawn manager
public int amount = 10;
public float interval = 1.0f;
private float lastTime;
//Time betwewen each wave
public float countdown = 10f;
//Does the actual spawning
public void ReadyToSpawn()
{
if(Time.time - lastTime >= interval)
{
SpawnManager.Instance.Spawn(spawnPrefabIndex, spawnPointIndex);
amount--;
lastTime = Time.time;
}
}
}
Answer by MarvinH · Feb 28, 2021 at 08:20 PM
woof crickets. Is there anything I can do to make this question more clear?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
What exactly is going on when I implement Update(), and other messages in MonoBehaviour 2 Answers
How to move a player automatically through a List of Waypoints? 1 Answer
Yet Another Bullet Decal Problem 0 Answers
How can i use any imported model(for example Crysis Nanosuit) instead of mecanim rigged model? 0 Answers