Enum as a function param in a button OnClick

Hi,

I have several buttons calling a function which parameter is an enum.

I can’t display the enum parameter in the OnClick() field, because my method appears missing.

Trying to serialize the enum doesn’t work.

Here is what I’m trying to obtain :

123950-enum.png

And here is my code :

[System.Serializable]
public enum Things { Thingie, Stuff, Whatever }
  
public void MyFunction(Things someThing)
{
    Debug.Log("Passed the parameter : " + someThing);
}

Thanx in advance !

The modern way is to set a class with a public enum. Pass along the class! This way also supports multiple parameters.
alt text

EDIT: I think enum may require [Serializable] attribute - forgot!
alt text

This is a bit old, but I had this problem just right now, so I’ll share my solution to it:

To avoid the problems of the upcast from ints, like the compatibility with other codes, the requirement of keep tracking the meaning of each number, or the problem if the enum get modified in the original order of the elements; and to avoid add another MonoBehaviour for each of the elements/buttons I have to create (so don’t overload the Unity engine with more starts/updates than those I need) using the “class with public enum property” solution, I’ve done another function as a wrap on the script to call for each of the enum options. In this way, the button only have to call the function for his own enum option, and add any other parameter if you need it too, ofc:

public void FuncWithEnum(EnumType enumType) {
    switch (enumType) {
        case EnumType.Value1:
            //Do something
            break;
        case EnumType.Value2:
            //Do something
            break;
    }
}
  
public void FuncWithEnumValue1() => FuncWithEnum(EnumType.Value1);
public void FuncWithEnumValue2() => FuncWithEnum(EnumType.Value2);

And now you can select it without problems:

202008-imagen1.png

202009-imagen2.png

The only problem with this it’s you have to modify the class and add a new function every time you add some new value to the enum, but at least that’s all and there is no problems of retro-compatibility with the rest of the code.

I hope this helps someone. Greetings :slight_smile:

Hey, enum values aren’t supported in the On Click delegate. To my knowledge, only integers and booleans are.

Simplest way i figured out is make method take string and try parse it

public enum YourEnum
{
     yourOption1,
     yourOption2
}
public class YourClass : MonoBehaviour
{
    private YourEnum enumState;

    public void SetEnum(string newState)
    {
        enumState = (YourEnum)Enum.Parse(typeof(YourEnum), newState);
    }
}

I just going to post here for fun but you can try to change the button instead.

//this is when the player hover on a button, it will play a hover clip.
public class CustomButton : MonoBehaviour , IPointerEnterHandler , IPointerDownHandler
{
    [SerializeField] private SFXClip clipToPlayWhenClick;
    [SerializeField] private SFXClip clipToPlayWhenHovered;
    public UnityEvent actions;
    //when the player hover on the button, play the hover clip
    public void OnPointerEnter(PointerEventData eventData)
    {
        SoundManager.Instance.PlayAudio(clipToPlayWhenHovered);
    }            
    
    //this is special case for the button for the connecting button.
    public void ClickSound()
    {
        SoundManager.Instance.PlayAudio(clipToPlayWhenClick);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        ClickSound();
        StartCoroutine(PlayingButtonBeforeActions());
    }

    private  IEnumerator PlayingButtonBeforeActions()
    {
        yield return new WaitForSecondsRealtime(1);
        actions?.Invoke();
    }
}

That way in your inspector, you can still use Enums.

I think that this question is worth to be answered because enums can be very useful in some cases. Certainly it’s better then to just use a string as param and then make a typo that causes problems.

This is the way I do it:

using UnityEngine;
  
public class MenuInvoker : MonoBehaviour
{
    private enum State { Main, Settings };
    [SerializeReference] private State state;
	public void InvokeMenu(MenuInvoker currState)
	{ 
            switch (currState.state)
            {
                case State.Main:
    			    Debug.Log("Main");
                break;
                case State.Settings:
    			    Debug.Log("Settings");
                break;
      
                default:
    			    Debug.Log("Default Menu");
                    break;
            }  
	}
}

well, i did it like this:
using int!

public enum BrickTypes { normal, idk };

public void SetType(int type) {
        current.BrickType = (BrickTypes)type;
}

Use upcast. @Koyemsi

public enum YourEnum { first = 0, second = 1, etc } 
public void ButtonActionOnClick (int someValue)
{
  switch( (YourEnum)someValue ) // use upcast, where 0 - first, 1 - second...
 {
   case(YourEnum.first):
   break;
 }
}