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

Question How to fetch public enum info?

Discussion in 'Scripting' started by RuneShiStorm, Dec 25, 2022.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi all!
    I have followed an Online tutorial and made a week-system.
    I've managed to "tap into" that week-system from my script, and can call the Debug "ItsMonday", which is very nice... It means I'm communicating with that script... Now...

    In the other script, there is a public enum DayOfWeek
    Code (csharp):
    1.  
    2. public enum DayOfWeek
    3. {
    4.     Sunday,
    5.     Monday,
    6.     Tuesday,
    7.     Wednesday,
    8.     Thursday,
    9.     Friday,
    10.     Saturday
    11. }
    12.  
    I want to be able to change the "" , in my script, to Monday, Tuesday etc, as shown below:

    Code (csharp):
    1.  
    2.  
    3.     DayTimeController dayTime;
    4.     public string CurrentDay = "";//Day
    5.  
    6.     void Start()
    7.     {
    8.         dayTime = GameManager.instance.dayController;
    9.     }
    10.  
    11.     public void Update()
    12.     {
    13.         if (Equals(DayOfWeek.Monday))
    14.         {
    15.             CurrentDay = "Monday";
    16.         }
    17.     }
    18.  
    19.  
    So I guess the question is: How do you comunicate with the enum so my "" <-Weekday is equals whatever the Enum is?
    Thankful for any help or pointers in the right direction!
     
    Last edited: Dec 25, 2022
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,396
    Have a reference to a DayOfWeek variable and then just do .ToString()
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    This is a very weird question to have. I have to guess that you know very little about enums in C#.
    First, make sure to understand what enums do. Here

    In short, by declaring enum values, you also define an enum type. A type in C# is what kind of content the variable is supposed to handle. In that sense enum declaration is pretty much similar to what class declaration does, BUT, while classes produce reference types, enum's type is a value type.

    This means two things in practice: 1) you don't have to use keyword
    new
    , as you can only pass enum literals around, and 2) enum literals are passed by value (i.e. copied on assignment).

    So you probably want your CurrentDay to be of DayOfWeek type and then you simply assign the value from DayController directly, no need for strings.

    But what does this mean?
    Code (csharp):
    1. if (Equals(DayOfWeek.Monday))
    This makes no sense.
     
  4. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Yes Im new to Enums.

    This
    code]if (Equals(DayOfWeek.Monday))[/code]
    is my attempt to make it work. but it doesn't. However, it doesn't leave any errors.
    So I thought I was on the right track, thus asking the community.

    In the DayTimeController script. The days change every 24 hours, and the Enum updates the Text on the UI, displaying the name of the day.
    What I want is to tap into that from other scripts, so I can, for example, activate an NPC every monday.
    THe link you provided says "You cannot define a method inside the definition of an enumeration type"
    Not sure what it means but sounds like I can't make a simple if (Equals(DayOfWeek.Monday)) - sound like I need to do something called extension method. But that look way over my paygrade :p Will look into it though, so thanks!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Generally stay away from enums if you contemplate Unity serializing them. Here's why:

    Enums enums are bad in Unity3D if you intend them to be serialized:

    https://forum.unity.com/threads/bes...if-not-do-something-else.972093/#post-6323361

    https://forum.unity.com/threads/unity-card-game-structure.1006826/#post-6529526

    It is much better to use ScriptableObjects for many enumerative uses. You can even define additional associated data with each one of them, and drag them into other parts of your game (scenes, prefabs, other ScriptableObjects) however you like. References remain rock solid even if you rename them, reorder them, reorganize them, etc. They are always connected via the meta file GUID.

    Collections / groups of ScriptableObjects can also be loaded en-masse with calls such as
    Resources.LoadAll<T>().
     
    RuneShiStorm likes this.
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,128
    You should be seeing an error like this:

    error CS0120: An object reference is required for the non-static field, method, or property 'object.Equals(object)'


    Something else to be aware of the '==' symbol and the Equals() method don't do the same thing. If you're trying to see if a variable is equal to a value you should use '=='. Equals() is to see if two variables are referencing the same object and doesn't care about the data stored in that object.

    https://www.tutorialsteacher.com/articles/equality-operator-vs-equals-method-in-csharp

    With that out of the way here is how you set a variable to an enum:
    Code (csharp):
    1. var day = DayOfWeek.Monday;
    Here is how you check if it's equal to a certain day:
    Code (csharp):
    1. if (day == DayOfWeek.Monday)
    If you want the text representation of an enum value from a variable and from the enum:
    Code (csharp):
    1. var text = day.ToString();
    2. var text = DayOfWeek.Monday.ToString();
     
    Last edited: Dec 26, 2022
    RuneShiStorm likes this.
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    The link I've provided is a reference manual to what enums are, how they're used etc. You're supposed to learn the language features before you attempt to use them. Or, I don't know, that's part of the help I'm offering -- a way to help you understand where your errors might be, because my answers would be too wordy, so here's the source of everything relevant about enums.

    Originally I wanted to provide a much more concrete answer to your setup, but after some thoughts, you were so far away from anything meaningful that I can't even tell what the hell you're doing. Do you want the string, do you simply want to match two enums, what is yours and what comes from some external API, who knows, you just assume it's clear from the example and it's not, if you pay attention to the answers, they're all over the place!

    So I figured out there is no other way but to subtly direct you to a place where you can learn all the basics, without having to talk about how little you know, because there is nothing useful about that.

    If learning about the tools you're using is above your paygrade, then how are we supposed to help you? You shouldn't be doing things above your paygrade, then waste our time trying to understand where you come from. I get "paygrade" is just a figure of speech, but learn more, improve your "paygrade", then try again, there are no shortcuts to this process.

    Extension methods are trivial btw (and are pretty neat in combination with enum types), but only when you know what you're doing with the straight C#. Learn your types, classes, methods, and control structures, these are the basics of any programming language. Then we can talk constructively.
     
    RuneShiStorm likes this.