Using enum

Please look at this code:

    enum Screen {MainMenu, Password, Win}
    
    Screen currentScreen=Screen.MainMenu;

    Screen currentScreen1;
    currentScreen1 = Screen.MainMenu;

It seems like the third and fourth line do the same job as the second. But on Visual Studio the fourth line gets errors.
Anyone…?

Hi Dani,

What is the error you are receiving?

Hi Rob. In the game there is no problem, but on VS the lines are decorated as an error.
image

Hi Dani,

…and if you hover over it, what is the error?

“does not exist in current context”

Hey Dani,

You can declare and initialise at the same time with a member variable, or you can initialise within a constructor or another method, but you cannot initialise at the class level separately like that.

This doesn’t just relate to enum, for example if you tried this;

using UnityEngine;

public class Example : MonoBehaviour
{
    public bool test = true;    // declare and initialise

    test = false;    // initialise only
} 

…youll see the same results.

You could do this;

using UnityEngine;

public class Example : MonoBehaviour
{
    public bool test;

    private void Awake() 
    {
        test = true;
    } 
} 

Hope this helps :slight_smile:

Greatly helps. Tnx!

1 Like

You’re very welcome Dani :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms