- Home /
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;
             }
         }
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;
I want it to be set as the current value, the one set in the inspector, how would I do that?
Untested, but this should be it.
 floorTypeInt = (sbyte)floorType;
I've tried that, but it just returned 0 no matter what.
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. 
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);
     }
 }
You can also use GetHashCode() over an enum.
Answer by Ailaxgt · Feb 06, 2013 at 10:07 PM
- 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". :)
- Did you do a - using System;at the top of the file? This is needed inorder to access the- Enumclass.
- Try the whole thing with - byte. Not sure why- sbyteis 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 :)
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
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                