Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to access a Enum in a Serializable class

Discussion in 'Scripting' started by Stew_79, Apr 29, 2021.

  1. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    Hi guys
    I am having difficulty accessing an enum in a Serializable class
    can someone help?

    Code (CSharp):
    1. [System.Serializable]
    2. public class Wave
    3. {
    4.     public enum waveType {Random, Pattern}
    5.     public waveType WaveType;
    6.     public string name;
    7.     public GameObject enemy;
    8.     public int count;
    9.     public float rate;
    10.    
    11. }
    12.  
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    What's wrong with the
    Wave.waveType.Pattern
    ?

    BTW, the enum should be capitalized (WaveType) and your

    Also: your enum should be WaveType (capitalized) and your variable should be wavetype. Since all other public variables of yours are non-capitalized.
     
  3. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    Hi thanks lurking Ninja
    I am trying to access the Enum from another class as follows

    Code (CSharp):
    1. if (Wave.WaveType == Wave.waveType.Random)
    2.         {
    3.             _enemy.transform.position = new Vector3(screenBounds.x + 10f, Random.Range(-screenBounds.y + 1f, screenBounds.y - 1f), 0f);
    4.             Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);
    5.         }
    6.         else if (Wave.WaveType == Wave.waveType.Pattern )
    7.         {
    8.             _enemy.transform.position = new Vector3(screenBounds.x + 10f, 0f, 0f);
    9.             Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);// uncommet to here
    10.         }
    But I am getting an error in Visual Studio stating that
    An object reference is required for the non-static field, method, or property 'Wave.WaveType'
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    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...
     
  5. Stew_79

    Stew_79

    Joined:
    Feb 24, 2021
    Posts:
    36
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Wave
    6.  
    7.  
    8. {
    9.     public enum WaveType {Random, Pattern}
    10.     public WaveType waveType;
    11.     public string name;
    12.     public GameObject enemy;
    13.     public int count;
    14.     public float rate;
    15.    
    16. }
    17.  
    18. public class WaveSpawner : MonoBehaviour {
    19.  
    20.     public enum SpawnState { SPAWNING, WAITING, COUNTING };
    21.  
    22.     public static int enemyCounter = 0; // im using static so I can access it from another class that gets destryoed
    23.     public static int waveCounteR = 0; // To be used to increse wave speed after all first waves  are complete
    24.  
    25.     Vector3[] npcMovment;
    26.     public enum enemyDirection { up, down};
    27.     public enemyDirection direction;
    28.     public Vector3 stopPosition;
    29.     bool xCordHolder = false;
    30.     int c = 0;
    31.     float yCord;
    32.     float loopCount = 0;
    33.     float newXcord = 0;
    34.     int counter = 0;
    35.     public float rate = 0;
    36.     float a = 0; // used in maths for instantiating enemy ships
    37.     float b = 0;
    38.     PlayerController _Controller;
    39.     GameController gameController;
    40.  
    41.     public Wave[] waves;
    42.    
    43.     private int nextWave = 0;
    44.     public bool waveSpawner; // use to turn off wavespawner
    45.     Vector2 screenBounds;
    46.     Vector3 currentPosition;
    47.     public int NextWave
    48.     {
    49.         get { return nextWave + 1; }
    50.     }
    51.  
    52.     public Transform[] spawnPoints;
    53.  
    54.     public float timeBetweenWaves = 5f;
    55.     private float waveCountdown;
    56.  
    57.     public float WaveCountdown
    58.     {
    59.         get { return waveCountdown; }
    60.     }
    61.  
    62.     private float searchCountdown = 1f;
    63.        private SpawnState state = SpawnState.COUNTING;
    64.  
    65.     public SpawnState State
    66.     {
    67.         get { return state; }
    68.     }
    69.  
    70.     void Start()
    71.     {
    72.      
    73.  
    74.         if (spawnPoints.Length == 0)
    75.         {
    76.             Debug.LogError("No spawn points referenced.");
    77.         }
    78.  
    79.         waveCountdown = timeBetweenWaves;
    80.         gameController = FindObjectOfType<GameController>();
    81.  
    82.      
    83.         yCord = 3.8f;
    84.  
    85.  
    86.     }
    87.  
    88.     void Update()
    89.     {
    90.  
    91.         screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    92.  
    93.         if (state == SpawnState.WAITING)
    94.         {
    95.             if (!EnemyIsAlive())
    96.             {
    97.                 WaveCompleted();
    98.             }
    99.             else
    100.             {
    101.                 return;
    102.             }
    103.         }
    104.  
    105.         if (waveCountdown <= 0)
    106.         {
    107.             if (state != SpawnState.SPAWNING && waveSpawner) /// braking wave spawner here with wave spawner
    108.             {
    109.                 StartCoroutine( SpawnWave ( waves[nextWave] ) );
    110.             }
    111.         }
    112.         else
    113.         {
    114.             waveCountdown -= Time.deltaTime;
    115.         }
    116.  
    117.     }
    118.  
    119.     void WaveCompleted()
    120.     {
    121.         Debug.Log("Wave Completed!");
    122.  
    123.         enemyCounter = 0;
    124.  
    125.         state = SpawnState.COUNTING;
    126.         waveCountdown = timeBetweenWaves;
    127.  
    128.         if (nextWave + 1 > waves.Length - 1)
    129.         {
    130.             nextWave = 0;
    131.             Reset();
    132.             Debug.Log("ALL WAVES COMPLETE! Looping...");
    133.         }
    134.         else
    135.         {
    136.             nextWave++;
    137.  
    138.             Reset();
    139.         }
    140.     }
    141.  
    142.     bool EnemyIsAlive()
    143.     {
    144.         searchCountdown -= Time.deltaTime;
    145.         if (searchCountdown <= 0f)
    146.         {
    147.             searchCountdown = 1f;
    148.             if (GameObject.FindGameObjectWithTag("Enemy") == null)
    149.             {
    150.              
    151.                 return false;
    152.             }
    153.         }
    154.         return true;
    155.     }
    156.  
    157.     IEnumerator SpawnWave(Wave _wave)
    158.     {
    159.    
    160.  
    161.         Debug.Log("Spawning Wave: " + _wave.name);
    162.         state = SpawnState.SPAWNING;
    163.        
    164.         for (int i = 0; i < _wave.count; i++)// use wave count to increment a counter to 3
    165.         {
    166.             SpawnEnemy(_wave.enemy, _wave.waveType );
    167.             rate = 1f / _wave.rate; // This is for NpCmoveMent
    168.             yield return new WaitForSeconds( 1f/_wave.rate );
    169.         }
    170.  
    171.         state = SpawnState.WAITING;
    172.  
    173.         yield break;
    174.     }
    175.  
    176.     void SpawnEnemy(GameObject _enemy, Wave.WaveType waveType)
    177.     {
    178.         //Debug.Log("Spawning Enemy: " + _enemy.name);
    179.  
    180.  
    181.         //Transform _sp = _enemy.transform;
    182.         //Instantiate(_enemy, _enemy.transform); // Spawer original instantiate method
    183.  
    184.         // a ++;
    185.         // b++;
    186.  
    187.         ////if (a >= screenBounds.x +30)
    188.         //// {
    189.         ////     a = -a;
    190.         //// }
    191.         //// if (a>= -screenBounds.x)
    192.         //// {
    193.         ////     a = +a;
    194.         //// }
    195.  
    196.         //if (b >= screenBounds.y)
    197.         //{
    198.         //    b -= b;
    199.         //}
    200.         //if (b >= -screenBounds.y)
    201.         //{
    202.         //    b = +b;
    203.         //}
    204.      
    205.  
    206.         Debug.Log( waveType );
    207.  
    208.         if (waveType == Wave.WaveType.Random)
    209.         {
    210.             _enemy.transform.position = new Vector3(screenBounds.x + 10f, Random.Range(-screenBounds.y + 1f, screenBounds.y - 1f), 0f);
    211.             Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);
    212.         }
    213.         else if (waveType == Wave.WaveType.Pattern)
    214.         {
    215.             _enemy.transform.position = new Vector3(screenBounds.x + 10f, 0f, 0f);
    216.             Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);// uncommet to here
    217.         }
    218.  
    219.  
    220.  
    221.         //test to see if we can spawn the path
    222.  
    223.  
    224.         //_enemy.transform.position = new Vector3(screenBounds.x + 10, -b, 0f);
    225.         //Instantiate(_enemy, _enemy.transform.position, _enemy.transform.rotation);
    226.  
    227.  
    228.  
    229.  
    230.         //  Transform _sp = spawnPoints[ Random.Range (0, spawnPoints.Length) ];
    231.         // Instantiate(_enemy, _sp.position, _sp.rotation);
    232.  
    233.         // Instantiate(_enemy,PositionT,Quaternion.identity );
    234.  
    235.  
    236.  
    237.     }
    238.  
    239.     public void EnneyDirectionController()
    240.     {
    241.         c++;
    242.    
    243.         if (c == 1)
    244.         {
    245.  
    246.             direction = enemyDirection.up;
    247.         }
    248.         if (c == 2)
    249.         {
    250.             direction = enemyDirection.down;
    251.             c = 0;
    252.         }
    253.      
    254.     }
    255.  
    256.     public Vector3 StopPosition  (float xCord) // Calculte The stop position for each enemy object
    257.     {
    258.        // xCord = Mathf.Round(xCord); // this is the xCord of the enemy object that is currently being moved to its stopPosition
    259.                                     // i think i want this xcord to not change untill 4 iterations have been completed
    260.                                     // 5f is the distance in which to move within
    261.         counter++;                  // Using a counter to make sure that we get 2 iterations before changing YCord
    262.  
    263.         if (xCordHolder == false) // First Start Only!!!!
    264.         {
    265.             newXcord = xCord - 5f;
    266.             xCordHolder = true;
    267.         }
    268.  
    269.         if (yCord <= 0.0f )          // we dont want anything at 0.0f on the ycord so we reset to screendounds.y - 1
    270.         {                                   // When yCord is at 0 change all this //
    271.             loopCount ++;
    272.             yCord = screenBounds.y -1.2f;
    273.             newXcord = xCord - 5f + loopCount;
    274.             Debug.Log(" xCord is " + xCord + " Loop Count is " + loopCount);
    275.  
    276.         }
    277.  
    278.         stopPosition = new Vector3(newXcord, yCord, 0f);
    279.  
    280.         if (counter == 2) // This is to make sure that we get 2 iterations before changing YCord
    281.         {
    282.             yCord = yCord - 1f;
    283.             counter = 0;
    284.         }
    285.  
    286.          return stopPosition;
    287.      
    288.     }
    289.  
    290.     private void Reset()
    291.     {
    292.        
    293.         xCordHolder = false;
    294.         loopCount = 0;
    295.         counter = 0;
    296.         yCord = screenBounds.y - 1f;
    297.     }
    298. }
    299.  
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    I'm glad you managed to get it working! Good luck with your learning journey!
     
    Stew_79 likes this.