Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by Koyemsi · Sep 04, 2018 at 04:24 PM · enum

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 function appears missing.

Trying to serialize the enum doesn't work.

Here is what I'm trying to obtain :

alt text

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 !

enum.png (15.8 kB)
Comment
Add comment · Show 4
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 eses · Sep 04, 2018 at 04:33 PM 1
Share

Hi,

See this (see comments too): https://feedback.unity3d.com/suggestions/allow-enums-with-onclick-buttons

...one way to go around this:
Assign a method that does not take parameters (or takes some parameter) and then calls some other method with enum, so assign to your OnClick your $$anonymous$$ethods like $$anonymous$$yFunctionThingie or $$anonymous$$yFunctionStuff and so on... use your methods as sort of in-between methods.

avatar image Koyemsi eses · Sep 04, 2018 at 05:06 PM 0
Share

Thanx for your quick answer !

Alternately, as enums work like arrays in some way (each element is indexed with an integer), I assume there should be a way to pass an int param and turn this into the corresponding enum.

Something like this (but I can't find the correct syntax):

 public void $$anonymous$$yFunction(int anInt)
      {
          Things someThing = Things[anInt]; // not the right syntax
          Debug.Log("The parameter is now: " + someThing);
      }
avatar image eses Koyemsi · Sep 04, 2018 at 05:35 PM 1
Share

@$$anonymous$$oyemsi

I'm not sure about the best method, at least you can cast int to enum, as it doesn't have manual numbering defined, the numbers from 0 and upwards will match. (Thingie = 0, Stuff = 1...)

 public void $$anonymous$$yFunction(int someThing)
 {
       var value = (Things)someThing;
       Debug.Log("Passed the parameter : " + value);
 }
 


BTW - this enum thing has been discussed in UI area of the forum. There's pretty long thread IIRC.

Show more comments

5 Replies

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

Answer by Any3Develop · Jan 10, 2020 at 07:15 AM

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;
  }
 }
Comment
Add comment · Show 4 · 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 Koyemsi · Jan 10, 2020 at 04:08 PM 0
Share

Yes, that's what I finally did. Thansl for your answer @Any3Develop

avatar image Bunny83 · Jan 11, 2020 at 02:06 AM 0
Share

Note that what you did is an explicit downcast. At the "top" is the base class. Below are derived classes. Upcasting is always possible implicitly and can never fail. Downcasting has to be explicit and can fail. Though in the case of int and an enum type it's more a conversion than an actual cast since value types do not support polymorphism at all.

avatar image Xavier78 · Feb 28, 2020 at 07:45 AM 2
Share

This is a bad practice. Enums can change values, and unless you specify values for each of your enums(which is actually suggested) things will break without you realizing. Also you are forcing anyone/yourself when you forget each value to have to look them up again.

avatar image notagame · Feb 10 at 08:00 PM 0
Share

Bad solution. The answer by MrLucid72 should have been the accepted one. Using ints is a horrible practice and it will backfire for sure.

avatar image
12

Answer by MrLucid72 · Aug 01, 2020 at 09:12 AM

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

Comment
Add comment · Show 4 · 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 JasonsFreeTime · Aug 06, 2020 at 02:04 AM 0
Share

@$$anonymous$$rLucid72 Interesting. This is something I've never done before but seems a good solution and powerful as well since as you said, it could support multiple parameters.

@Bunny83 Curious what you think of this solution.

Just as and Example: For anybody that wondered how this converts to a switch statement, in my project I just created this as my component:

 using UnityEngine;
 
 public class PanelsComponent : $$anonymous$$onoBehaviour
 {
     public Panels Panels;
 }

and this is the method OnClick() will trigger:

     public void SelectPanel(PanelsComponent panel)
     {
         foreach (PanelGameObjects p in panelGameObjects)
         {
             p.Panel.SetActive(false);
         }
 
         switch (panel.Panels)
         {
             case Panels.$$anonymous$$apSettings:
                 panelGameObjects.Find(x => x.PanelType == Panels.$$anonymous$$apSettings).Panel.SetActive(true);
                 break;
             case Panels.EmitterSettings:
                 panelGameObjects.Find(x => x.PanelType == Panels.EmitterSettings).Panel.SetActive(true);
                 break;
         }
     }
avatar image chrisoje · Oct 01, 2020 at 05:35 PM 0
Share

Works perfectly! Cleanest and best solution. Thank you!

avatar image crazyrems · Dec 02, 2020 at 02:21 PM 0
Share

You can also use ScriptableObjects instead of $$anonymous$$onoBehaviors so you don’t have to add a component to any GameObject and it’s reusable.

avatar image notagame · Feb 10 at 08:01 PM 0
Share

Thanks, this is a cool solution to another episode of Unity's "How do I fuckup the most basic flows" specialty.

avatar image
4

Answer by nathanlink169 · Sep 04, 2018 at 07:30 PM

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

Comment
Add comment · Show 4 · 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 Koyemsi · Sep 04, 2018 at 11:25 PM 1
Share

That's the conclusion I came too. It's a lack.

avatar image Koyemsi · Sep 05, 2018 at 12:23 PM 0
Share

Strings are also supported.

avatar image e-motionagency · Sep 19, 2019 at 02:28 PM 1
Share

One wonders how ScriptableObjects are supported, but not enums ?!

avatar image Bunny83 e-motionagency · Jan 11, 2020 at 01:52 AM 0
Share

Because the ArgumentCache only has those concrete types:

  • Object m_ObjectArgument;

  • string m_ObjectArgumentAssemblyTypeName;

  • int m_IntArgument;

  • float m_FloatArgument;

  • string m_StringArgument;

  • bool m_BoolArgument;

avatar image
1

Answer by Laaste · Aug 11, 2020 at 07:28 PM

Simplest way i figuredout 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);
 }

}

Comment
Add comment · 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
0

Answer by Hex0dus · Jun 22, 2020 at 07:00 AM

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;
         }
 
     }
 }
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 EZaca · Aug 03, 2020 at 10:53 PM 1
Share

How would you use that? Add a script, then reference the script?

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

163 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change the value of a timer from another script? 1 Answer

Hide fields depended on enum selection 0 Answers

How do I work with two ENUMS 2 Answers

How do check which enum value was selected? 2 Answers

How apply something like the "!" Operator to an enum? 2 Answers


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