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;
    		}
    	}

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;
  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”. :slight_smile:

  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 :slight_smile: