Search Unity

[Solved ]Having trouble accessing enums...

Discussion in 'Scripting' started by Aeskyphaz, Feb 19, 2018.

  1. Aeskyphaz

    Aeskyphaz

    Joined:
    Jun 2, 2017
    Posts:
    52
    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 (unity3d.com/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 :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TestEnums : MonoBehaviour {
    6.  
    7. public enum Letter
    8. {
    9.     A,
    10.     B,
    11.     C,
    12.     D,
    13.     E
    14. }
    15. }
    16.  
    My "game manager" :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.     public class TestGameManager : MonoBehaviour {
    6.     // Use this for initialization
    7.     public Letter _letter; //tried both in public & private
    8.  
    9. //'The type or namespace name 'Letter' could not be found (are you missing a using directive or an assembly reference?)
    10.  
    11. }
    12.  
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FalseMatchActor : MonoBehaviour {
    6.  
    7.     void Start(){
    8.         TestGameManager tgm = GameObject.Find("something").GetComponent<TestGameManager>();
    9.     }
    10.      void PlayerDoesSomethingLikeMove(){
    11.         //move action
    12.         tgm._letter = Letter.A;
    13.      }
    14. }
    15.  
    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
     
    Last edited: Feb 19, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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.
     
    Aeskyphaz likes this.
  3. Aeskyphaz

    Aeskyphaz

    Joined:
    Jun 2, 2017
    Posts:
    52
    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
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    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:
    Code (CSharp):
    1. public enum Letter
    2. {
    3.     A,
    4.     B,
    5.     C,
    6.     D,
    7.     E
    8. }
     
    TaleOf4Gamers likes this.
  5. Aeskyphaz

    Aeskyphaz

    Joined:
    Jun 2, 2017
    Posts:
    52
    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!