How to call an enum from another script with an enum
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
Your example is missing the crucial line or i can't find it because the formatting is a little random. "//error says: Only assignment, call, increment, decrement, and new object expressions can be used as a //statement spawnW;" what is "spawnW;"? If it is the variable, then just set it to something like spawnW = SpawnState.Spawning;
I was trying to declare spawnW as my variable. I tried something similar in this line:
//trying to access SpawnState enum from other script WGen.SpawnState spawnW = WGen.SpawnState.Waiting;
So, I should just take out the WGen part and make it:
spawnW = SpawnState.Waiting;
is that correct?
It looks like your script is checking if there are remaining spawns before it starts a new wave continuously, so i guess you would either have to destroy existing enemies, or just i guess what you want is set the state variable: just make this "private SpawnState state = SpawnState.Counting;" public and do [referencetothespawnercomponent].state = SpawnState.Spawning. But keep in $$anonymous$$d that you're overwriting the scripts checks that way, so depending on your use cases it could break things.
Answer by Blue-Cut · Apr 14, 2016 at 05:46 PM
Hello,
Your code is very long so I am a bit confused, but to call an enum from 1 script in another, you need to write the name of the class that contains the enum.
// In script 1
public class ClassEnum
{
public enum MyEnum { TYPE1, TYPE2, TYPE3 }
}
// In script 2
public class ClassCaller
{
public void SomeFunction()
{
ClassEnum.MyEnum var = ClassEnum.MyEnum.TYPE1
}
}
But I saw something like this in your code, so I am not sure where your problem is. Can you please edit your question to make your code cleaner and more readable ? By selecting relevant extracts and be carefull with the comment typo.
Answer by N1warhead · Apr 14, 2016 at 11:13 AM
I'm not even gonna read all your code. As I just had this issue earlier today, I found a work around as I couldn't figure it out either.
All I did was create a new Method (In C#).
public void WhatEver(string newEnumValue){
if(newEnumValue == "WhatEver"){
enum = Enum.Whatever;
}
}
So pretty much what's going on here, we send a string value from the other script. And if the string value = whatever you want, then run the Enum as you would inside the same script already.
Dont do that (this is too error prone). If you want to set an enum just pass the enum value.
$$anonymous$$yEnum {None,Something} public void Set$$anonymous$$yEnum($$anonymous$$yEnum enumValue){ myEnumField = enumValue; } or have an argument-less method, that just sets it to the required value.
@ValooFX Thanks again! I really appreciate your help here!
@N1warhead I really appreciate the reply. Every little bit helps.
Your answer

Follow this Question
Related Questions
SceneManager: how can I find GameObject in another scene? 1 Answer
Where can i find package manger?,I cannot find package manager in my unity? 1 Answer
Damage manager (ThirdPersonShooter) 1 Answer
color is not changing in enum code 0 Answers
Using Direction enum to move an enemy from off screen 0 Answers