Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    25
    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. Deleted User

    Deleted User

    Guest

    UNITY HOW IS THIS FEATURE STILL MISSING
     
    Darkziss, ksf000, awsapps and 6 others like this.
  4. VirtualPierogi

    VirtualPierogi

    Joined:
    Sep 3, 2012
    Posts:
    54
    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:
    28
    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:
    50
    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:
    37
    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:
    8
    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:

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

    fantiusen

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

    Marks4

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

    skeyll

    Joined:
    Dec 14, 2018
    Posts:
    3
    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.
     
    TomTrottel 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:
    28
    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.
     
    unnanego and awsapps like this.
  16. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    Please Unity Devs, add this feature!
     
    unnanego likes this.
  17. Vivraan

    Vivraan

    Joined:
    Feb 2, 2018
    Posts:
    26
    +1
     
    awsapps and unnanego like this.
  18. Vivraan

    Vivraan

    Joined:
    Feb 2, 2018
    Posts:
    26
    Refactored version containing the latest supported C# syntax rules.
     

    Attached Files:

    SAM-tak and Kaideu like this.
  19. thephoenix007

    thephoenix007

    Joined:
    Nov 29, 2018
    Posts:
    7
    They have stopped responding, Another Bump, Its 2022, 3 years and it will be 10 years of asking for a simple fracking enum in a unityaction. Jesus Christ on a cracker, unity can't afford a fracking 3 point story card to add something people have been wanting for almost a decade?
     
    awsapps, Menion-Leah and unnanego like this.
  20. TomTheMan59

    TomTheMan59

    Joined:
    Mar 8, 2021
    Posts:
    348
    Maybe the reason is they are moving on to UIToolkit and uGUI is going to be replaced in like 5 years? but idk...:(
     
    awsapps and unnanego like this.
  21. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    Seven years for a feature like this definitely sounds unbelievable.
     
    Privateer, awsapps and unnanego like this.
  22. Privateer

    Privateer

    Joined:
    Jun 5, 2016
    Posts:
    23
    Definitely. I get that it might not be easy but aren't seven years enough?
     
    awsapps and Menion-Leah like this.
  23. Kaideu

    Kaideu

    Joined:
    Nov 6, 2018
    Posts:
    5
    It's been many moons and the featureland still seems nowhere insight.
    Rations have dwindled and winters are harsh
    People have started to lose hope as they slowly stop returning while the rest of us lay in wait for the update that may never be..
     
    QuantumSno, Alex-CG, Marks4 and 2 others like this.
  24. rsodre

    rsodre

    Joined:
    May 9, 2012
    Posts:
    229
    I'm considering getting a job in Unity just to make this happen.
     
  25. ContxtualDempsey

    ContxtualDempsey

    Joined:
    Mar 7, 2022
    Posts:
    3
    LOL what a Joke, had to go through the whole thing to see if they ever replied again.

    Three responses over 8 years, 0 action.
     
    AlonMixed, rsodre, saltysquid and 3 others like this.
  26. Shezraan

    Shezraan

    Joined:
    Mar 23, 2023
    Posts:
    3
    Here's another refactored version of @llamagod's original answer.
     

    Attached Files:

    mabrima, Alex-CG and AlonMixed like this.
  27. Richard_Blackwidow

    Richard_Blackwidow

    Joined:
    Dec 29, 2021
    Posts:
    6
  28. Raseru

    Raseru

    Joined:
    Oct 4, 2013
    Posts:
    87
    Very nice, is there an easy way to also extend this to EventTriggers? Like on PointerUp, do this event?
     
  29. s140335

    s140335

    Joined:
    Feb 19, 2020
    Posts:
    4
    9 years have passed. looks like Unity just ignored this.
     
    unnanego and Menion-Leah like this.
  30. StrangeMonsterZ

    StrangeMonsterZ

    Joined:
    Feb 21, 2021
    Posts:
    1
    Don't worry guys, they have all their forces aimed at a new and improved contract for small indie creators. That's what our community really needs!
     
  31. devCioo

    devCioo

    Joined:
    Sep 27, 2022
    Posts:
    1
    2023 ending and it's still not implemented. That feels like a joke to me.
     
    unnanego and Menion-Leah like this.
  32. CSEliot

    CSEliot

    Joined:
    May 9, 2014
    Posts:
    33
    bumpedy arumpadump, when can we see, the frumpedy content bump!
     
  33. MarcoCarandenteDCL

    MarcoCarandenteDCL

    Joined:
    Oct 27, 2020
    Posts:
    2
    Happy new year to this feature request! This year, you're going to turn 10! Are you excited?