[Solved ]Having trouble accessing enums...

Hello there
I’ve been trying to get used to enums; my plan was to have a GameManager with several states in a turn per turn game (PlayerMoving, SomethingHappening…). For that I wanted to use an enum, whose state could change for instane when my player moves
However when I try to access the GameManager state enum from my player class to change it, it appears that it does not exist…
I’ve found info (Learn, uunity forums, stack overflow) about putting the enum on a different class (which a did), changing my class names, making sure the enum is public, and so on, but still no way to access it…
I tried on some new scripts, looking like that:

My separate enum class :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestEnums : MonoBehaviour {

public enum Letter
{
    A,
    B,
    C,
    D,
    E
}
}

My “game manager” :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class TestGameManager : MonoBehaviour {
    // Use this for initialization
    public Letter _letter; //tried both in public & private
 
//'The type or namespace name 'Letter' could not be found (are you missing a using directive or an assembly reference?)
 
}

As stated in comment I can’t access the Letter enum, no matter what I try… so my plan which was to do this, from a player class, can’t possibly work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FalseMatchActor : MonoBehaviour {

    void Start(){
        TestGameManager tgm = GameObject.Find("something").GetComponent<TestGameManager>();
    }
     void PlayerDoesSomethingLikeMove(){
        //move action
        tgm._letter = Letter.A;
     }
}

Could anyone help me change the enum state of an A class, by calling a function on a B class ?
And just to be clear, I did search quite a lot, but every example or explanation I found resulted in the same thing : OP saying “Thanks, works”, but the result ended up giving me the very same error that I get here when I repeat the process (even through raw copy/paste)… So i’m probably doing a conceptual mistake but I can’t see where

Thanks in advance
Aes

Your enum is part of the class TestEnums. So you have to access it through that class. TestEnums.Letter _letter

Also note, if you use Visual Studios, when it shows that red line under Letter, you can right click on it, click the light bulb and it will give you some options on how to resolve the error if it can figure it out. In which case it would show this solution to you. If you use monodevelop, I suggest you switch to another ide.

I’ll not get into which is better, as that’s a huge debate in itself, but I myself have never had good experiences with monodevelop.

1 Like

Thanks, using “public TestEnums.Letter _letter;” in the 2nd script (TestGameManager) and replacing " tgm._letter = Letter.A; " with “tgm._letter = TestEnums.Letter.A;” made it compile just fine !

Since I didnt expect such a quick answer i’ll check how that applies to my actual game situation tomorrow (it’s late out here) and mark the question as solved if it works

Thanks for your quick and helpful answer (and the lightbulb tip in VS, i’m definitely dropping MonoDevelop), I feel like I see the enums syntax more clearly now
Regards,
Aes

A few quick notes:
Your TestEnum class shouldn’t inherit Monobehaviour. Your just using it to hold various enums. It doesn’t need to have a Start(), an Update() and all of that.

Also its perfectly acceptable to have an enum sitting out by itself. Your original code would have worked if you had created a file called LetterEnum.cs and had it look like this:

public enum Letter
{
    A,
    B,
    C,
    D,
    E
}
1 Like

Okay so everything now works fine for me, thanks
I know it shouldn’t inherit from monoheaviour, I just copied/pasted a quick snippet from default file for demonstration purpose
Thanks for pointing out that it can sit out by itself, not sure why people were mentioning it shouldn’t in some earlier posts I found ! I applied this to my file

Issue solved, thanks again!