Hi guys
I am having difficulty accessing an enum in a Serializable class
can someone help?
[System.Serializable]
public class Wave
{
public enum waveType {Random, Pattern}
public waveType WaveType;
public string name;
public GameObject enemy;
public int count;
public float rate;
}
You aren’t just trying to access the enum, you’re trying to access the WaveType variable as well (which happens to be wavetype enum).
I urge you to rethink your naming convention… even I had to reread my sentence above and I deal with junk code on a daily basis…
So, you can access the enum (type) from anywhere, just like I said above in my previous post. In order to actually read the variable (WaveType), you will need to have an object where you have that variable. Sadly you truncated your script, so I don’t know what you’re doing there. I’m guessing you have these Wave objects instantiated and serialized somewhere…
I have now made the changes you suggested. I have now manged to get it working. I am modifying a tutorial script for spawning enemy’s. My problem was due to me not passing waveType to my coroutine which is used to spawn the enemy’s. Here’s The full script. I am new to this and trying to learn as I go.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Wave
{
public enum WaveType {Random, Pattern}
public WaveType waveType;
public string name;
public GameObject enemy;
public int count;
public float rate;
}
public class WaveSpawner : MonoBehaviour {
public enum SpawnState { SPAWNING, WAITING, COUNTING };
public static int enemyCounter = 0; // im using static so I can access it from another class that gets destryoed
public static int waveCounteR = 0; // To be used to increse wave speed after all first waves are complete
Vector3[] npcMovment;
public enum enemyDirection { up, down};
public enemyDirection direction;
public Vector3 stopPosition;
bool xCordHolder = false;
int c = 0;
float yCord;
float loopCount = 0;
float newXcord = 0;
int counter = 0;
public float rate = 0;
float a = 0; // used in maths for instantiating enemy ships
float b = 0;
PlayerController _Controller;
GameController gameController;
public Wave[] waves;
private int nextWave = 0;
public bool waveSpawner; // use to turn off wavespawner
Vector2 screenBounds;
Vector3 currentPosition;
public int NextWave
{
get { return nextWave + 1; }
}
public Transform[] spawnPoints;
public float timeBetweenWaves = 5f;
private float waveCountdown;
public float WaveCountdown
{
get { return waveCountdown; }
}
private float searchCountdown = 1f;
private SpawnState state = SpawnState.COUNTING;
public SpawnState State
{
get { return state; }
}
void Start()
{
if (spawnPoints.Length == 0)
{
Debug.LogError("No spawn points referenced.");
}
waveCountdown = timeBetweenWaves;
gameController = FindObjectOfType<GameController>();
yCord = 3.8f;
}
void Update()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
if (state == SpawnState.WAITING)
{
if (!EnemyIsAlive())
{
WaveCompleted();
}
else
{
return;
}
}
if (waveCountdown <= 0)
{
if (state != SpawnState.SPAWNING && waveSpawner) /// braking wave spawner here with wave spawner
{
StartCoroutine( SpawnWave ( waves[nextWave] ) );
}
}
else
{
waveCountdown -= Time.deltaTime;
}
}
void WaveCompleted()
{
Debug.Log("Wave Completed!");
enemyCounter = 0;
state = SpawnState.COUNTING;
waveCountdown = timeBetweenWaves;
if (nextWave + 1 > waves.Length - 1)
{
nextWave = 0;
Reset();
Debug.Log("ALL WAVES COMPLETE! Looping...");
}
else
{
nextWave++;
Reset();
}
}
bool EnemyIsAlive()
{
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("Enemy") == null)
{
return false;
}
}
return true;
}
IEnumerator SpawnWave(Wave _wave)
{
Debug.Log("Spawning Wave: " + _wave.name);
state = SpawnState.SPAWNING;
for (int i = 0; i < _wave.count; i++)// use wave count to increment a counter to 3
{
SpawnEnemy(_wave.enemy, _wave.waveType );
rate = 1f / _wave.rate; // This is for NpCmoveMent
yield return new WaitForSeconds( 1f/_wave.rate );
}
state = SpawnState.WAITING;
yield break;
}
void SpawnEnemy(GameObject _enemy, Wave.WaveType waveType)
{
//Debug.Log("Spawning Enemy: " + _enemy.name);
//Transform _sp = _enemy.transform;
//Instantiate(_enemy, _enemy.transform); // Spawer original instantiate method
// a ++;
// b++;
////if (a >= screenBounds.x +30)
//// {
//// a = -a;
//// }
//// if (a>= -screenBounds.x)
//// {
//// a = +a;
//// }
//if (b >= screenBounds.y)
//{
// b -= b;
//}
//if (b >= -screenBounds.y)
//{
// b = +b;
//}
Debug.Log( waveType );
if (waveType == Wave.WaveType.Random)
{
_enemy.transform.position = new Vector3(screenBounds.x + 10f, Random.Range(-screenBounds.y + 1f, screenBounds.y - 1f), 0f);
Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);
}
else if (waveType == Wave.WaveType.Pattern)
{
_enemy.transform.position = new Vector3(screenBounds.x + 10f, 0f, 0f);
Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);// uncommet to here
}
//test to see if we can spawn the path
//_enemy.transform.position = new Vector3(screenBounds.x + 10, -b, 0f);
//Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);
// Transform _sp = spawnPoints[ Random.Range (0, spawnPoints.Length) ];
// Instantiate(_enemy, _sp.position, _sp.rotation);
// Instantiate(_enemy,PositionT,Quaternion.identity );
}
public void EnneyDirectionController()
{
c++;
if (c == 1)
{
direction = enemyDirection.up;
}
if (c == 2)
{
direction = enemyDirection.down;
c = 0;
}
}
public Vector3 StopPosition (float xCord) // Calculte The stop position for each enemy object
{
// xCord = Mathf.Round(xCord); // this is the xCord of the enemy object that is currently being moved to its stopPosition
// i think i want this xcord to not change untill 4 iterations have been completed
// 5f is the distance in which to move within
counter++; // Using a counter to make sure that we get 2 iterations before changing YCord
if (xCordHolder == false) // First Start Only!!!!
{
newXcord = xCord - 5f;
xCordHolder = true;
}
if (yCord <= 0.0f ) // we dont want anything at 0.0f on the ycord so we reset to screendounds.y - 1
{ // When yCord is at 0 change all this //
loopCount ++;
yCord = screenBounds.y -1.2f;
newXcord = xCord - 5f + loopCount;
Debug.Log(" xCord is " + xCord + " Loop Count is " + loopCount);
}
stopPosition = new Vector3(newXcord, yCord, 0f);
if (counter == 2) // This is to make sure that we get 2 iterations before changing YCord
{
yCord = yCord - 1f;
counter = 0;
}
return stopPosition;
}
private void Reset()
{
xCordHolder = false;
loopCount = 0;
counter = 0;
yCord = screenBounds.y - 1f;
}
}