Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Cobradabest · Feb 06, 2013 at 09:00 PM · c#programmingenum

Problem with getting a value from a enum

I'm trying to get a value from an enum, so that another script can reference it, and play a sound, the problem is, for example, 1 is for a wood step sound, and 2 is for the metal sound, I start off with walking on a Metal surface, then move to a wooden one, it still plays the metal step sound, and the value doesn't change, it stays at 2.

What am I doing wrong?

Here's my code:

     public enum FloorType : byte
     {
         metal = 0,
         wood = 2,
         glass = 3,
     }
     public FloorType floorType;
     public static byte floorTypeInt;
     public static bool walkingOnSurface;
 void Update ()
     {
         floorTypeInt = (byte)floorType;
         //print(floorTypeInt + ", " +(int)floorType);
     }
     // Use this for initialization
     void OnTriggerEnter (Collider floor)
     {    
             if (floor.collider.gameObject.tag == "Player")
             {
                 walkingOnSurface = true;
             }
     }
     void OnTriggerExit (Collider floor)
     {    
             if (floor.collider.gameObject.tag == "Player")
             {
                 walkingOnSurface = false;
             }
     }

WalkSound Script Code:

 if ((Input.GetAxis("Horizontal") != 0 ||
                     Input.GetAxis("Vertical") != 0) && WalkonGround.walkingOnSurface)
                 {
                     typeOfFloor = (sbyte)WalkonGround.floorTypeInt;
                     print(typeOfFloor + " " + WalkonGround.floorTypeInt);
                     WalkSound();
                 }
         void WalkSound()
         {
             if (walkTimer <= 0)
             {
                 audio.PlayOneShot(walkingSound[typeOfFloor]);
                 walkTimer = walkCooler;
             }
         }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Feb 06, 2013 at 09:58 PM

I wouldn't bother with the sbyte unless you are going to have a huge number of them, and it is possible that you will pay a price doing it this way. You will need to add a "using System;" at the top of your file to access "Enum". In addition, GetValues() returns an array of values, not a single value. Maybe you want to do something like:

 floorTypeInt = (sbyte)FloorType.wood;
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cobradabest · Feb 06, 2013 at 10:17 PM 0
Share

I want it to be set as the current value, the one set in the inspector, how would I do that?

avatar image robertbu · Feb 06, 2013 at 10:50 PM 0
Share

Untested, but this should be it.

 floorTypeInt = (sbyte)floorType;
avatar image Cobradabest · Feb 06, 2013 at 10:54 PM 0
Share

I've tried that, but it just returned 0 no matter what.

avatar image Wolfram · Feb 06, 2013 at 11:03 PM 1
Share

Forget the byte. $$anonymous$$emory will usually be aligned to 32bit anyways (and even 64bit for 64bit builds), and I assume Unity ergo C# ergo .NET is doing the same thing. So unless you have at least 4 byte variables that are declared consecutively and the compiler is intelligent enough to pack these into one single dword, don't bother.

avatar image robertbu · Feb 07, 2013 at 01:49 AM 4
Share

I just tested the following and it worked fine:

 public class EnumTest : $$anonymous$$onoBehaviour {
     public enum FloorType
     {
        metal = 0,
        wood = 2,
        glass = 3,
     }
  
     public FloorType floorType;
     private int floorTypeInt;
     void Awake()
     {
        floorType = FloorType.glass;
        floorTypeInt = (int)floorType;
        Debug.Log(floorTypeInt);
     }
 }
avatar image raulssorban robertbu · Apr 15, 2017 at 09:35 PM 0
Share

You can also use GetHashCode() over an enum.

avatar image alxcancado robertbu · Sep 04, 2017 at 08:57 PM 0
Share

Perfect, just what I needed.

Cheers!

Show more comments
avatar image
1

Answer by Ailaxgt · Feb 06, 2013 at 10:07 PM

  1. the statement typeof(floorType) is not correct. The typeof method needs a type not a variable. In your case it was just the difference between a "floorType" and "FloorType". :)

  2. Did you do a using System; at the top of the file? This is needed inorder to access the Enum class.

  3. Try the whole thing with byte. Not sure why sbyte is not working.

    public enum FloorType:byte { metal = 0, wood = 2, glass = 3, }

      public FloorType floorType;
         public static byte floorTypeInt;
         void Awake()
         {
             floorType = FloorType.glass;
            floorTypeInt = (byte)floorType;
            print(floorTypeInt);
         }
    
    
    

Hope this helps :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cobradabest · Feb 06, 2013 at 11:21 PM 0
Share

I've tried the code, it doesn't work, for some reason, the "(byte)floorType", always returns 0 no matter what I choose.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I'm having troubles using a Capsule Cast 2 Answers

Semicolons are seen as an unexpected symbol 1 Answer

Problem with 3D platform tutorial 2 Answers

Instantiating Prefabs through Editor Script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges