- Home /
adding wave number to HUD
Using a script from a tutorial I found on YouTube by Brackeys and its just spawning enemies and I select which enemies and how many per wave. problem is I cant work out how to add the wave name to my HUD tried so many different options and none of it seems to be working. need the _wave.name displayed in HUD. Ignore all the comments that's just me trying to understand everything since very new to coding :D help would definitely make my day thanks.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WaveSpawner : MonoBehaviour {
public enum SpawnState { SPAWNING, WAITING, COUNTING };
[System.Serializable] // allows us to change values inside of inspector
public class Wave
{
public string name; //name of wave
public Transform enemy; // refrence to prehab we want to initiate
public int count; // amount of waves
public float rate; // spawn rate
}
public Wave[] waves;
private int nextWave = 0;
public int NextWave
{
get { return nextWave + 1; }
}
public Transform[] spawnPoints;
public float timeBetweenWaves = 5f; //time between waves 5 seconds
private float waveCountdown;
public float WaveCountdown
{
get { return waveCountdown; }
}
private float searchCountdown = 1f; //instantiate to check if all enemy are dead every 1 second instead of every frame
private SpawnState state = SpawnState.COUNTING;
public SpawnState State
{
get { return state; }
}
void Start()
{
if (spawnPoints.Length == 0)
{
Debug.LogError("No spawn points referenced."); // checks if a spawn point of refrenced
}
waveCountdown = timeBetweenWaves; //how long till next wave starts default 5seconds
}
void Update()
{
if (state == SpawnState.WAITING) //checks if enimies are still alive
{
if (!EnemyIsAlive())
{
WaveCompleted(); // if all enimies dead start next wave
}
else
{
return; // if enemies still alive return till all enimies dead
}
}
if (waveCountdown <= 0) //if <=0 then start check if spawning of not start spawning then else
{
if (state != SpawnState.SPAWNING)
{
StartCoroutine( SpawnWave ( waves[nextWave] ) ); //start spawning wave
}
}
else
{
waveCountdown -= Time.deltaTime; //makes countdown relevent to time and not frames per second
}
}
void WaveCompleted()
{
Debug.Log("Wave Completed!");
state = SpawnState.COUNTING;
waveCountdown = timeBetweenWaves;
if (nextWave + 1 > waves.Length - 1) // if no more waves start wave 1 again looping
{
nextWave = 0;
Debug.Log("ALL WAVES COMPLETE! Looping..."); // if final wave not completed add 1 wave
}
else
{
nextWave++; //increases wave by 1
}
}
bool EnemyIsAlive()
{
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f; //check if all enemy are dead every 1 second instead of every frame
if (GameObject.FindGameObjectWithTag("Gooba") == null) //checks if all enimies with the player tag enemy are alive or dead
{
return false; // if enemy alive repeat step till enemies are dead check this every 1 second
}
}
return true; // if all enemy with tag enemy are dead then return true
}
IEnumerator SpawnWave(Wave _wave)
{
Debug.Log("Spawning Wave: " + _wave.name);
state = SpawnState.SPAWNING; //spawns enimes
for (int i = 0; i < _wave.count; i++) // = number of enimies we want to spawn
{
SpawnEnemy(_wave.enemy); //waits for player to finish killing enimies
yield return new WaitForSeconds( 1f/_wave.rate ); //how long we want to wait 1 / rate untill we have number of enimies we want to spawn
}
state = SpawnState.WAITING;
yield break;
}
void SpawnEnemy(Transform _enemy)
{
Debug.Log("Spawning Enemy: " + _enemy.name);
Transform _sp = spawnPoints[ Random.Range (0, spawnPoints.Length) ]; //chooses a random spawn point
Instantiate(_enemy, _sp.position, _sp.rotation);
}
}
Your answer
Follow this Question
Related Questions
How to display EULA from Doc in Unity? 0 Answers
Problem Displaying Score on GUI text 0 Answers
Displaying varying text 1 Answer
GUI Overlay Display 0 Answers
Simple text GUI 2 Answers