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

Enum value's forwarding, backwarding?

Discussion in 'Scripting' started by leegod, Sep 7, 2014.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,321
    hi.

    So what I want is, as I manipulate my 'turn' int value +-, I want to change also today's day of the week.

    Is there more neat way to do this?

    Here is code.

    Code (CSharp):
    1. enum Days{
    2.     Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
    3. }
    4.  
    5. int turn = 1;
    6. Days today;
    7.  
    8.  
    9. void NextDay(){
    10.         if((turn -1) % 7 != 0)
    11.             ++today;
    12.         else
    13.             today = Days.Sunday;
    14.     }
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Yup there sure is.

    Something like:

    Code (CSharp):
    1. today = (Days)turn;
    or even:

    Code (CSharp):
    1. today = Enum.ToObject(typeof(Days), turn);
     
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,321
    hi,

    but error says, 'The name `Enum' does not exist in the current context'

    and I did like this and its done.

    today = (Days)((turn-1) % 7);
     
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    of sorry if you wanted to use my way you'd have to add the namespace System.Enum
     
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Code (csharp):
    1.  
    2. Using System;
    3.  
    This line placed at the top of your script(s) will resolve Enum namespace issues.

    ~
    Another walk in the park.
     
  6. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,321
    using System.Enum also result error when compilie in unity.
     
  7. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,321
    using System;

    Also result following error.

    Cannot implicitly convert type `object' to `Days'.
    An explicit conversion exists (are you missing a cast?)
     
  8. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    try this:

    System.Enum.ToObject(typeof(Days), turn);

    if you need to cast it then try: System.Enum.ToObject(typeof(Days), turn) as Days;
     
  9. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Code (csharp):
    1. enum Days
    2. {
    3.     Sunday = 0, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
    4. }
    5.  
    6. Days today = Days.Sunday;
    7.  
    8. void NextDay()
    9. {
    10.     today = (Days)((int)(today + 1) % 7); // forward
    11.     today = (Days)((int)(today - 1 + 7) % 7); // backward
    12. }
     
    Last edited: Sep 13, 2014
  10. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,321
    Yours good! Works.