I’m having a little trouble trying to put these two scripts together for my enemy wave spawner. Can anyone help me with properly calling this enum? I’ve been going through this all day. Thanks in advance!
**First Script:**
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum SpawnState {Spawning, Waiting, Counting};
public static class WGen : MonoBehaviour {
[System.Serializable]
public class Wave
{
public string name;
public GameObject enemy;
public int spawnCount;
public float wDelay;
public Transform[] spawnPoints; // where enemy will spawn
}
public Wave[] waves;
private int nextWave = 0;
public float timeBetweenWaves = 5f;
public float waveCountdown;
//search for enemies on screen to happen only once per second, to avoid overtaxing the game
private float searchCountdown = 1f;
private SpawnState state = SpawnState.Counting;
void Start()
{
waveCountdown = timeBetweenWaves;
}
void Update()
{
if(state == SpawnState.Waiting)
{
// -2- Them if we are waiting, we should check if all enemies are destroyed by player or Game
if(!EnemyIsAlive())
{
// -3- If none are alive, then we begin a new round
Debug.Log("Wave Completed!");
return;
}
else
{
// -4- if enemies are still alive, then return
return;
}
}
//1) Check to see if we're at 0 in our countdown yet (should not be) **
if (waveCountdown <= 0)
{
//2) once we reach 0, we'll check if we're already spawning **
if (state != SpawnState.Spawning)
{
//3) if we're not already spawning then we will start spawning wave **
StartCoroutine(SpawnWave (waves[nextWave]));
}
else
{
{
//4) So we'll subtract from our countDown until we reach 0 **
waveCountdown -= Time.deltaTime;
}
}
}
}
bool EnemyIsAlive()
{
//Countdown from preset time of 1 second to 0 seconds, before starting
searchCountdown -= Time.deltaTime;
if(searchCountdown >= 0f)
{
//reset searchCountdown
searchCountdown = 1f;
//Check to see if there are any enemies left with on the screen by using their tag as a reference
if(GameObject.FindGameObjectWithTag("Enemy") == null)
{
//if there are none, return false
return false;
}
}
//otherwise, return true
return true;
}
IEnumerator SpawnWave(Wave _wave)
{
Debug.Log("Spawning Wave: " + _wave.name);
//5) That will put us in our Spawning State **
state = SpawnState.Spawning;
//7) Loop through the number of enemies that we want to spawn **
for(int i = 0; i < _wave.spawnCount; i++)
{
SpawnEnemy(_wave.enemy);
//8) Then, wait a certain amount of seconds before looping through the IEnumerator again **
yield return new WaitForSeconds(_wave.wDelay);
}
// -1- After Spawning, set the SpawnState to Waiting
state = SpawnState.Waiting;
yield break;
}
//6) For each enemy that we want to spawn, we call the SpawnEnemy method to instantiate an enemy **
void SpawnEnemy(GameObject _enemy)
{
//Spawn Enemy
Debug.Log("Spawning Enemy: " + _enemy.name);
// Only pick a new spawn point once per wave
Instantiate(_enemy, transform.position, transform.rotation);
}
}
**Second script (the one I'm having trouble with):**
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public static class GameManager : MonoBehaviour {
//Reference to game objects
public GameObject playButton;
public GameObject playerShip;
public GameObject enemySpawner; // reference to enemy spawner
public SpawnState spawnState; //reference to SpawnState enum in WGEN
public GameObject GameOverGO;
public GameObject scoreUITextGO; // reference to score UI
public enum GameManagerState
{
Opening,
Gameplay,
Intermission,
GameOver,
}
GameManagerState GMState;
//public enum.SpawnState spawnState { get; set;}
// Use this for initialization
void Start () {
GMState = GameManagerState.Opening;
//trying to access SpawnState enum from other script
WGen.SpawnState spawnW = WGen.SpawnState.Waiting;
}
// Update is called once per frame
void UpdateGameManagerState () {
switch(GMState)
{
case GameManagerState.Opening:
//set play button to visible (active)
playButton.SetActive(true);
break;
case GameManagerState.Gameplay:
//Reset the score
scoreUITextGO.GetComponent<GameScore>().Score = 0;
//hide play button on game play state
playButton.SetActive(false);
//Set player shooting to Invoke Repeating
playerShip.GetComponent<Player>().PlayerFiring();
//set player to visible (active) and init the player lives
playerShip.GetComponent<Player>().Init();
//Trying to start enemy spawner, but I'm having trouble here
//error says: Only assignment, call, increment, decrement, and new object expressions can be used as a //statement
spawnW;
break;
case GameManagerState.GameOver:
//TODO: Stop enemy spawner, once I figure out how to start it
//change game manager state to Opening state after 5 second delay
Invoke("ChangeToOpeningState", 5f);
break;
}
}
public void SetGameManagerState(GameManagerState state)
{
GMState = state;
UpdateGameManagerState();
}
public void StartGamePlay(){
GMState = GameManagerState.Gameplay;
UpdateGameManagerState();
}
public void ChangeToOpeningState()
{
SetGameManagerState(GameManagerState.Opening);
}
}
I’ve looked through multiple answers for questions that were similar to mine, but either my case is a little different or I’m just not getting it. Any help is greatly appreciated. Been trying to work this out for a month now.
Thanks again!
Milan