Search Unity

Question I have attached a button click to a static method (impossible?)

Discussion in 'Editor & General Support' started by ColinBroderick, Jun 7, 2023.

  1. ColinBroderick

    ColinBroderick

    Joined:
    Mar 1, 2022
    Posts:
    9
    I have attached a button click to static method in a static class but I don't know how I did it and can't do it again.

    I have a static class called EventManager, containing a static method called RaisePlayLogEvent.

    upload_2023-6-7_14-57-6.png
    upload_2023-6-7_14-57-16.png

    I have a button which claims to have nothing attached to its onClick.

    upload_2023-6-7_14-58-37.png

    However, in the scene file, the button DEFINITELY is attached to that class and method, and 100% most definitely the button works when I click it. It passes the expected argument where it needs to go and the expected effect is observed to happen.

    upload_2023-6-7_15-1-53.png

    I am at a loss as to how this has happened, and how I do it again. Everything I read says that onClick cannot be attached to a static method. And yet ...

    Any ideas, thoughts, suggestions?
     
  2. ColinBroderick

    ColinBroderick

    Joined:
    Mar 1, 2022
    Posts:
    9
    There are no other components attached to the button, other than the expected Image, RectTransform, and CanvasRenderer.
     
  3. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Don't know the answer to your question re: static methods, but if you set the inspector to Debug Mode you might be able to see & modify the event call there.
     
    ColinBroderick likes this.
  4. ColinBroderick

    ColinBroderick

    Joined:
    Mar 1, 2022
    Posts:
    9
    Thanks for that, didn't know that debug mode was there. Yes, it does show the class, method, and argument you would expect.

    upload_2023-6-7_15-22-26.png
     
    flashframe likes this.
  5. ColinBroderick

    ColinBroderick

    Joined:
    Mar 1, 2022
    Posts:
    9
    (And yes, based on a test just now, it is possible to "trick" Unity into letting me assign a static method this way, not that it's necessarily advisable...)

    However this definitely isn't how I achieved it the first time round since I didn't even know this was possible.
     
  6. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Perhaps you assigned it in code somewhere?
     
  7. ColinBroderick

    ColinBroderick

    Joined:
    Mar 1, 2022
    Posts:
    9
    I don't think so - searched all files and no occurrences of onclick or addlistener anywhere in my code that might explain it. Also no occurrences of the method name anywhere you wouldn't expect. Likewise for other buttons showing similar behaviour.
     
  8. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Are you using version control? Could look through the changesets to try to find when it was modified.

    Ps. I think static function calls are supported (well clearly they are!) - just not in the Unity Editor. i.e. there's no built in support for hooking them up / drawing them in the editor.
     
  9. ColinBroderick

    ColinBroderick

    Joined:
    Mar 1, 2022
    Posts:
    9
    Yes, I am using version control. I'll check out some old commits tomorrow when I have more time and see if I can work out what's changed. (This is a project that was last touched about a year ago so I definitely can't remember).

    Thanks for the assistance so far. I'm at the point where I can achieve what I need to for today, but have a decision to make going forward - probably don't want to continue using a method that only works with the editor in debug mode!
     
  10. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    You could wrap the static method in a non-static function in a script somewhere in your scene (perhaps on the same object as the button)? Not ideal, but at least it would show up correctly in the inspector.

    Good luck!!
     
  11. ColinBroderick

    ColinBroderick

    Joined:
    Mar 1, 2022
    Posts:
    9
    Thanks flashframe. I did think about that - it is a common suggestion when Googling this kind of issue - but I don't totally love it from a design perspective. It might be the less evil option though.

    I think I have worked out how I got into this situation. The EventManager class was originally a MonoBehaviour attached to an object, which I had forgotten. At some point I removed it and converted EventManager into a static class. Presumably at that time everything continued to work as expected so I never looked any deeper. The object reference for the button's OnClick was lost, but it seems to have retained all the other information, though not readily visible in the editor.
     
    flashframe likes this.