3
\$\begingroup\$

I have the following enum:

public enum WeaponType { Sword, Spear, Blunt, Ranged }

And I have the following public methods on a component:

public void MyMethod1(int myparam) { ... }
public void MyMethod2(WeaponType myparam) { ... }

Why is it that when I try to call my methods from a Button component, I can not see the ones that use enumerations as parameters?

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ You can't, Unity's inspector isn't set up to handle it. Good news, integers are directly convertable to enum values. \$\endgroup\$ – Draco18s no longer trusts SE Aug 1 '17 at 21:45
  • \$\begingroup\$ @Draco18s How? Also please consider post this as an answer. :) \$\endgroup\$ – jeromej May 15 '18 at 8:25
  • \$\begingroup\$ @Draco18s Albeit easy to Google (as a matter of fact I already did and found it), it's better to give off the info directly. Same-ish policy as "no link-only answers". In case you ain't coming back, y'all only need casting: (EMyEnum)myInt (works both way round too!) \$\endgroup\$ – jeromej May 15 '18 at 9:14
  • \$\begingroup\$ @JeromeJ Comments don't count as answers and aren't subject to the same rules. \$\endgroup\$ – Draco18s no longer trusts SE May 15 '18 at 15:49
2
\$\begingroup\$

As Draco18s stated in comments, sadly, you can't.

Thankfully, if it helps, you can still just cast it back and forth:

EMyEnum myEmum = (EMyEnum)myInt;

// Or.
int myInt = (int)myEnum;
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.