Search Unity

Ability to add enum argument to button functions

Discussion in 'UGUI & TextMesh Pro' started by Slev, Sep 26, 2014.

  1. guicamarotto

    guicamarotto

    Joined:
    Jun 23, 2020
    Posts:
    19
    Hello all, I was looking for an example to how do this enum on click button and came into this thread and want to share my solution.

    First I create this script
    Code (CSharp):
    1. public enum StateList
    2. {
    3.     Move,
    4.     Attack,
    5.     Skill,
    6.     Item,
    7.     Wait
    8. }
    9.  
    10. public class ChooseActionButtons : MonoBehaviour
    11. {
    12.     public StateList stateList;
    13.     protected StateController sm { get { return StateController.instance; } }
    14.  
    15.     public void ActionButtons()
    16.     {
    17.         switch (stateList)
    18.         {
    19.             case StateList.Move:
    20.                 //sm.ChangeTo<MoveTargetState>();
    21.                 break;
    22.             case StateList.Attack:
    23.                 //sm.ChangeTo<ChooseTargetState>();
    24.                 break;
    25.             case StateList.Skill:
    26.                 //sm.ChangeTo<ChooseTargetState>();
    27.                 break;
    28.             case StateList.Item:
    29.                 //sm.ChangeTo<ItemSelectionState>();
    30.                 break;
    31.             default:
    32.                 //sm.ChangeTo<WaitState>();
    33.                 break;
    34.         }
    35.     }
    36.  
    37.  
    38. }
    So I added this script to each button I have, and chose my enum on the script editor. Drag my own button to the click event, and on the event list I chose my script and the method "ActionButtons". and works, I get the enum item chosen on the script editor.

    enumbutton.PNG
     
    unnanego likes this.
  2. issac8a

    issac8a

    Joined:
    Aug 29, 2017
    Posts:
    2
    This should make it work, at least in Unity 2019.4 works fine, I just removed those lines and gave directly a new Unity Event(), actually this feature is quite easy to implement, thanks @llamagod for the code, I am using it quite often.
     

    Attached Files:

    JOni likes this.
  3. MagpieStudios

    MagpieStudios

    Joined:
    Jun 30, 2018
    Posts:
    17
    UNITY HOW IS THIS FEATURE STILL MISSING
     
  4. VirtualPierogi

    VirtualPierogi

    Joined:
    Sep 3, 2012
    Posts:
    53
    unity could just buy the odin inspector or ultEvents and get back to investing in ml and xr
     
  5. ledshok

    ledshok

    Joined:
    Oct 28, 2012
    Posts:
    23
    This is a nice solution that I've adopted myself. Thank you for sharing it!
     
    guicamarotto likes this.
  6. StrongCube

    StrongCube

    Joined:
    Nov 22, 2016
    Posts:
    51
    Unity could have bought a lot of popular assets a long time ago, the same Amplify and do not make your wretched Shader Graph... but this is Unity :DDD
     
  7. maewionn

    maewionn

    Joined:
    Jan 18, 2016
    Posts:
    32
    Haha, on the first page, it was stated that this was added to the feautre requests. That was in 2014 and it's still missing. Oh Unity...
     
    G4M5T3R, Marks4 and unnanego like this.
  8. anand27dixit

    anand27dixit

    Joined:
    Jun 22, 2020
    Posts:
    1

    Hi I have used this as explained but in the unity console it is showing an error NullReferenceException "Object reference not set to an instance of an object". I have attached screenshot to the same. Please help me.
     

    Attached Files:

  9. josh_dreemar

    josh_dreemar

    Joined:
    Jan 9, 2019
    Posts:
    7
    For those who need it, this version worked for me in 2020.2. Seems one of the UnityEventBase internal functions was altered to accept a System.Type instead of a System.Object.
     

    Attached Files:

    HeshamAkmal, G4M5T3R and Dark-1-Games like this.
  10. fantiusen

    fantiusen

    Joined:
    Jan 7, 2018
    Posts:
    1
    Unity 2021 and this BASIC function it's still missing...
     
    Snowdrama likes this.
  11. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    324
    WHY IS THIS STILL MISSING? FFS!
     
    Snowdrama likes this.
  12. skeyll

    skeyll

    Joined:
    Dec 14, 2018
    Posts:
    2
    I changed UnityEventDrawer to this code( #159 ) and now it works! Thanks!
    Unity 2019.4.17 -> 2020.3.7
     
    G4M5T3R likes this.
  13. Alex-CG

    Alex-CG

    Joined:
    May 10, 2015
    Posts:
    11
    Maybe most of the people that worked at Untiy when this thread was created are not there anymore, seven years later.
     
    dasparadoxon likes this.
  14. G4M5T3R

    G4M5T3R

    Joined:
    Jan 10, 2015
    Posts:
    10
    The thread is still pretty active. Haven't enough people have requested this (and/or shared their solutions) to implement this natively? C'mon guys :p

    Implementing this natively appears to be more complicated than I thought. llamagod's solutions works great for my use case. I'm using 2020.3
     
    Last edited: Jul 31, 2021
  15. Snowdrama

    Snowdrama

    Joined:
    May 10, 2014
    Posts:
    20
    This whole thread is brilliant! I'm glad there's SOME solution that works. Thanks everyone here for their hard work! Appreciated!

    To the Unity Devs, PLEASE look into adding this feature, it really should be there, I'm glad I have this workaround but It'd be great if a workaround wasn't needed.
     
    awsapps likes this.
  16. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    40
    Please Unity Devs, add this feature!
     
unityunity