Search Unity

[Solved] UI Button not working at all

Discussion in 'UGUI & TextMesh Pro' started by Sargaxon, Aug 25, 2016.

  1. vejab

    vejab

    Joined:
    Dec 21, 2021
    Posts:
    119
    The panel on which the buttons were placed had a Canvas component. Removing this component solved the issue for me.
     
  2. Juan_Manuel_Rider_Jurado

    Juan_Manuel_Rider_Jurado

    Joined:
    Nov 19, 2021
    Posts:
    2
    I had this same problem although it was none of the above, in my case I deleted by mistake the EventSistem GameObject, I'm working in the 2020.3.14f1 version and if you don't have an EventSistem GameObject, one is automaticaly created when you create a canvas
     
    Soselo likes this.
  3. Juan_Manuel_Rider_Jurado

    Juan_Manuel_Rider_Jurado

    Joined:
    Nov 19, 2021
    Posts:
    2
    I was in your exact position and I solved it, I'm the #102 comment
     
  4. Wesleywalstonxx

    Wesleywalstonxx

    Joined:
    Nov 8, 2021
    Posts:
    1
    I Had a problem when I switched scenes and I couldn't use my UI buttons anymore, but I was brainstorming and I had an idea on how to fix it and it worked it was so unbelievably simple. Use a cursor lock state and a Cursor lock mode Confined, and for the people who don't know what I'm talking about heres the code. Lost File - Microsoft Visual Studio 1_10_2022 11_56_04 PM.png
     
  5. milantelt

    milantelt

    Joined:
    Jul 1, 2021
    Posts:
    1
    I removed my EventSystem so stupid, adding it back helped me.
     
    MichaelEGA likes this.
  6. nathan2019spence

    nathan2019spence

    Joined:
    Jan 21, 2022
    Posts:
    19
    Be careful, if you use the the prefab third person controller it contains its own event system, and will make your buttons stop working. grrr
     
  7. unity_w36k9MNcB9_bxg

    unity_w36k9MNcB9_bxg

    Joined:
    Jan 23, 2022
    Posts:
    1
    Resolveu meu problema! Obrigado.
     
  8. pythontest456

    pythontest456

    Joined:
    Jan 29, 2022
    Posts:
    1
    for me it was loading a scene through script
    this line specific: SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    hope this would save some time to a few
     
  9. LightID

    LightID

    Joined:
    Mar 11, 2021
    Posts:
    3
    Thank you!!!! The problem is that if you lock your cursor, 'There are no guarantees of where a locked cursor should be on the screen. Thus we(the devs) have removed the interactability of a locked cursor with the UI.'
     
    Last edited: Mar 16, 2022
    Lekteroi and pedroarruda4991 like this.
  10. leanhquang2803

    leanhquang2803

    Joined:
    Dec 18, 2021
    Posts:
    2
    I had the same issue. This is my solution:
    1. Go to the parent game object of the button.
    2. Reorder the button to the lowest position of the parent game object.
    3. Test out your button again.
    Why? Some other UI components or game objects might be above your button and won't allow you to click the button.
     
  11. kevinkevin1

    kevinkevin1

    Joined:
    Mar 3, 2022
    Posts:
    1
    I was able to fix mine by moving the event system outside of the canvas. I have to canvas and one of them contains the event system, but I am enabling and disabling that canvas for certain reasons. Adding multiple eventsystem inside canvases would also work but an alert message would pop up. One event system is enough, just ensure that it can be accessible by all UI components
     
  12. ninagbr

    ninagbr

    Joined:
    Apr 4, 2022
    Posts:
    1
    If it is something you changed within your project, my solution was
    to deactivate all components on the scene and enabling them one by one.
    I checked to see when button works and when not, and I found that the problem was with one particular object which had an "override layer" (sth sth dont remember) on the Tab/Layer/Module tab on the inspector (the one on the very top). I reset the settings there and the problem was solved.
     
  13. johntitortheprogrammer

    johntitortheprogrammer

    Joined:
    Jul 26, 2021
    Posts:
    1
    guys i realized my problem. if you bind a camera to any canvas and if you close it in the game later you should do overlay mode to canvas
     
  14. Max_dragotoba

    Max_dragotoba

    Joined:
    Nov 16, 2021
    Posts:
    1
    make sure that the parent and the button itself have a scale of 1. having the scale at 0 was my problem.
     
  15. NpcBoss

    NpcBoss

    Joined:
    Dec 9, 2021
    Posts:
    3
    Its the Event System hahahah, i used it before but forgot to add at the next scene, how stupid lol
     
  16. Japppe

    Japppe

    Joined:
    Sep 9, 2013
    Posts:
    1
    Thanks alot, worked like a charm after adding it back!

    Just create an new empty game object -> Add component "Event System"
     
  17. Arsenyokh

    Arsenyokh

    Joined:
    Aug 23, 2022
    Posts:
    1
    If you had this problem, baybe you delete EventSystem. You can add it Hierarchy>> right mouse button>> UI>> EventSystem
     
  18. hammerboard

    hammerboard

    Joined:
    Dec 21, 2020
    Posts:
    1
    Thx to this hint which led me found that my Event System Game Object was missing. Once I created in the main scene buttons work.
     
    ayhaab-pasha likes this.
  19. ABalniko

    ABalniko

    Joined:
    Apr 16, 2022
    Posts:
    1
    My problem was I put canvas for the inventory, etc.. under the canvas, not panels. As soon as I replaced them with a panel, my problem was solved.
     
  20. nudz-klecany

    nudz-klecany

    Joined:
    Oct 6, 2015
    Posts:
    3
    OK, so I encountered a very strange behavior. Following NO CHANGES in the scene containing the UI one particular UI button simply stopped working. After reverting all (absolutely unrelated) changes via source control, the UI button still not working, even if the scene is EXACTLY the same as before when it was clearly working. Hopeless found this thread and tried deleting and recreating the event system (which BTW looks exactly the same as it did before), voila button suddenly working!!! So happily redid all the changes that I previously reverted due to an unsuccessful attempt to fix the UI and guess what - the very same button NOT WORKING AGAIN!!! So if you ever want to recreate this bug simply create two Inspector windows while in the prefab variant edit mode, lock one of them and watch your UI break for no apparent reason. For me happened in Unity 2019.4 but not sure if it does not happen in other editor versions as well...
     
  21. Avatar-Vick

    Avatar-Vick

    Joined:
    Oct 1, 2020
    Posts:
    5
    I had a similar issue. Creating an empty GameObject inside the Canvas element and nesting sliders, toggles, etc. within it caused problems.

    Removing the UI input elements and making them direct children of the Canvas GameObject fixed everything.
     
  22. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    144
    This is killing me.

    I have a script that is supposed to allow a non-VR user to click on Objects on a PC monitor while another user is in VR. The PC user clicking the objects triggers events and the script has been working for weeks but suddenly stopped.
    I have checked my physics settings and matrix.
    The Object has a rigid body and collider that becomes active on hover,
    The layer and tags are set.
    No other colliders blocking the RaycastHit.
    The camera has a Physics Raycaster Max Ray Intersection 0. Event Mask is set to everything.
    There are no errors but I don't even get debug logs when clicking on the objects.

    Here is the script which was working and works in a backup scene on another machine but after adding new interactions it suddenly stopped working:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3.  
    4. public class MouseClickDoX : MonoBehaviour
    5. {
    6.  
    7.    public UnityEvent mouseClickEvent;
    8.    private GameObject button;
    9.  
    10.    private void Start()
    11.    {
    12.        button = this.gameObject;
    13.    }
    14.  
    15.    private void Update()
    16.    {
    17.  
    18.        if (Input.GetMouseButtonDown(0))
    19.        {
    20.            RaycastHit hit;
    21.            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    22.  
    23.            if (Physics.Raycast(ray, out hit, 1000.0f) && hit.collider.gameObject == button)
    24.            {
    25.                Debug.Log("Hit collider: " + hit.collider + ", target: " + button);
    26.                Debug.Log("hit.collider == target" + hit.collider + button);
    27.                if (hit.transform != null)
    28.                    //mouseClickEvent.Invoke();
    29.                    CurrentClickedGameObject(hit.transform.gameObject);
    30.            }
    31.        }
    32.    }
    33.  
    34.    public void CurrentClickedGameObject(GameObject gameObject)
    35.    {
    36.        if (gameObject.tag.Contains("button"))
    37.        {
    38.            mouseClickEvent.Invoke();
    39.        }
    40.    }
    41. }
    I have a canvas with UI buttons that I can click and activate the interactable objects but those objects are no longer registering a raycast. Hovering the objects has an effect but clicking them does not.


    I have tried everything in this thread.
    There seems to be a strange bug in Unity where copying and pasting a button can break things in the event system. It seems some users were able to fix this by deleting and recreating the Event system or changing toggles in the event system however non of that worked for me. Luckily I had a backup from minutes before the issue started. I went back to that backup on the same machine and the issue persisted. I had the backup on a flash drive and tested it on another machine and it worked fine. I was able to move that flash file back to the primary machine and it works again in both files so somehow I tricked the bug into working again. After about 4 hours of playtesting no issues and no changes to code or any objects in the scene the bug suddenly returned after closing and reopening Unity.
    I'm in Unity 2020.3.29 and could really use some ideas on how to fix this.
    Thanks.

    P.S. It still works on my laptop it just glitches and stops working on my desktop. Is there a cache file holding some corrupted unity setting that I could delete or something?

    EDIT:
    I tried using iPointerHandler script instead of raycast hit but it didn't work either.
    I ended up having issues with monobehavior not reading and in my code editor and even though the pugging where active and what not. I had to uninstall Unity and all versions then reinstall delete .csproj/.sln files and library folder and things still dent work.

    I finally got the iPointerClick Handler to work by adding a blank OnPointerUp() and OnPointerDown() which is not mentioned as needed in the Unity docks but once I added them things finally worked.?!
     
    Last edited: Oct 24, 2022
  23. Ligmaphobia

    Ligmaphobia

    Joined:
    Mar 27, 2022
    Posts:
    9
    I have read all comments from the first page(#50)
    Nothing. Tried everything exept redoing everything, which would take me about 2h and will probably bug again when I restart Unity.

    It worked perfectly. I closed unity and went to sleep. Next day I launch unity and it doesn't work at all. Then I tried everything above. still nothing.

    There are no unnecessary components, or GameObjects; There is only one canvas and only one Event system at the top of the hierarchy, both with correct components and values (Even tried different combos); Everything is z = 0, canvas is 10 (tried 100 and 1000 to).

    Attached my hierarchy
    I have buttons only on the PauseMenu (panel) object - I enable it via script when escape is pressed
     

    Attached Files:

  24. Giantbean

    Giantbean

    Joined:
    Dec 13, 2012
    Posts:
    144
    What's your script look like? Are you using the new input system the old or both? Are you ray casting UI buttons, game Objects or both? It seems that if all of the steps from previous posts have been done and if its not a case of a bad Event system or where iPointerHandler script should be used instead of raycast then this issue may need a ticket filed as it sometimes works until Unity reboot and then fails without explanation.
     
  25. Kadudu3K

    Kadudu3K

    Joined:
    Dec 9, 2022
    Posts:
    1
    The button's text should have its ray cast checkbox (text mesh pro, extra settings) turned off so it stops blocking it. Also have event system thingy.
     
  26. vBayKer

    vBayKer

    Joined:
    Oct 13, 2019
    Posts:
    1
    I had this problem, but I realised it was because I accidentally deleted my "EventSystem" that comes when you add the Canvas.
     
  27. kowcp91

    kowcp91

    Joined:
    Sep 15, 2022
    Posts:
    1
    Hi everyone,
    In my case it is Starter Asset player controller to blame. Once I disabled whole imported player placeholder the buttons are working fine. Still looking for solution. If I find any I'll let you know.
     
  28. pooja7hks

    pooja7hks

    Joined:
    Jan 23, 2023
    Posts:
    1
    you're awesome!!!!!!!
     
  29. Allennick

    Allennick

    Joined:
    May 22, 2020
    Posts:
    3
    If you are using the Starter asset input template and your UI is not working make sure to uncheck the "Cursor locked" checkbox inside the "Starter Assets Input" script. I've been messing with my UI for two days before it wasn't working. Now it works.
     
  30. DrVanillaCookie

    DrVanillaCookie

    Joined:
    Aug 23, 2021
    Posts:
    53
    I'll add my story to the list: I have FadeInOut object in the hierarchy, which is basically a UI Image (a black rect) and I modify it's transparency to create fade in/out. And for some stupid reason I made it a canvas, so it added Graphic Raycast component. So it was blocking the cursor from the rest of UI.
     
  31. Hozgen90

    Hozgen90

    Joined:
    Jan 20, 2021
    Posts:
    19
    i had the same problem and i solved my problem removing the tick on Raycast Target. But there was also another problem because if you have more instances related to the same TMP file it will not work properly. In this case you have to create another material to prevent them from conflicting with each other. Create another materil for TMP, Remove Tick from Raycast Target option, and all will be fine^^
     
  32. Pl3en

    Pl3en

    Joined:
    Jun 23, 2015
    Posts:
    1
    I had the same problem. My layers:

    CANVAS
    Panel <-- needed a RayCaster
    Button
     
  33. iheartfunnyboys

    iheartfunnyboys

    Joined:
    May 6, 2023
    Posts:
    1
    I was having the same issue as listed here and didn't see the solution suggested, but was able to fix mine and wanted to share in case anyone else is still stuck with their UI button not working in WebGL build after working in the Unity Editor.

    In my case, I have a Don't destroy on load - global logic script that, among other things, populates a scene with a various number of buttons based on player data at a given moment and navigates to new scenes. Late in the dev process after my game has already been working and tested in WebGL format, I added new buttons that just would not work. I finally figured out what was wrong with the buttons -- unlike all others, these have their OnClick defined within the Don't destroy on load logic script, rather than in a standalone script attached to somewhere in the scene they are placed in. The logic was so simple, just changing scenes, that I didn't think it was worth it's own script for one line.

    So yeah, check that the button's On Click's are being defined in the scene they exist in, not the Don't destroy scene. Mine literally just point into that Don't destroy global logic script, but needed to exist local to the scene.
     
  34. Gamer_Tom

    Gamer_Tom

    Joined:
    Apr 10, 2023
    Posts:
    5
    In my case it was the canvas sorting order. Setting it to 1 made the buttons work.
     
  35. JohnSoby

    JohnSoby

    Joined:
    Sep 17, 2014
    Posts:
    1
    A simple delete of the EventSystem and then creating a fresh one solved it for me. Two hours wasted troubleshooting. :p
    Thanks for y'all's input!
     
  36. nikolakis831656

    nikolakis831656

    Joined:
    Nov 6, 2022
    Posts:
    2
    for me it just was my text i had i put 5000 in height and width and if you check in the scene tab and select it it is covering all of my buttons so i just resized it and it worked
     
  37. mertmars

    mertmars

    Joined:
    Nov 24, 2021
    Posts:
    1
    hi,
    in my situation,
    there were two scenes loaded...ui objects in second scene were blocking raycasts for first scene...
    then i removed the "graphic raycaster" component from the canvas in second scene...and it's solved...
    you can keep that in mind...
     
  38. alibenkhalifa26

    alibenkhalifa26

    Joined:
    Sep 14, 2023
    Posts:
    1
    I hope you live a long and happy life.
     
  39. afrias90

    afrias90

    Joined:
    Sep 19, 2023
    Posts:
    2
    Seeing as this took me quite a while, i thought i would share my solution. I had two issues.

    Before that *In case anyone else is new like me, I want to add that you can Play the scene and add/remove components to troubleshoot and when to stop, none of the changes will be saved.

    1. I had my buttons blocked but another image in my UI (that i thought should have been deactivated). I was able to deactivate the image in Play mode and see that i need to move my buttons down the UI list (to the front).
    Try adding highlighted cover to your buttons to see if you're even sensing the buttons, delete or simply uncheck other objects in Play Mode and try clicking on buttons.

    2. Second, I needed to add the Graphic Raycaster system to the Buttons (Vertical layout Group) object that neatly ordered my buttons. In Play mode, i also found the minimum requirement was the Event System in my UI object that housed my buttoms and the Graphic Raycaster in the Vertical Layout Group object - the parent that holds my buttons.


    I'm using Unity 2022.3
     
  40. wechat_os_Qy0xDfW7_FXo5lVaICGD12FD8

    wechat_os_Qy0xDfW7_FXo5lVaICGD12FD8

    Joined:
    Jul 9, 2023
    Posts:
    6
    Tried lots of ways, finally solving it by moving the button component to the same object of the Target Graphic. The button's click-event would not be triggered if I put the button component into a individual game object
     
  41. ddocherty2303

    ddocherty2303

    Joined:
    Feb 15, 2023
    Posts:
    1
    Bit of info that might save a life, cause it certainly saved mine. I had my cursor locked through a completely separate script. Never even considered it since when you hit esc. in the editor it brings your mouse back.
     
  42. ZeluxeL

    ZeluxeL

    Joined:
    Sep 16, 2023
    Posts:
    1
    I'm experiencing this issue now and it's so bad because I'm making the game for a class and the game is due on Friday.

    I deleted one of the UI elements that I had just added... and it worked. Ok, but that's not really a viable solution... Is there seriously a limit to how many UI elements Unity can handle? If so, they shouldn't charge $.20 for each download or whatever.

    Please tell me there's some way to fix this besides removing UI elements, but the chance that anyone will reply to this before Friday (Dec. 1, 2023) are slim to none...

    Edit: Never mind, I misunderstood the issue. I had the buttons I was trying to access within a panel and somehow that made them not work. When I moved them directly into the canvas they worked. But yeah, it was weird because it messed up the whole UI, not just those elements.

    Before (didn't work): https://drive.google.com/file/d/1qXxZcgwiwQfxanChgv41iXb-9Gq819Nv/view?usp=sharing

    After (works): https://drive.google.com/file/d/1W-txK82DLbB8AmeltLgB5cfczlKkR2JZ/view?usp=sharing

    Edit 2: I'm still encountering the issue, only now it's only affecting one element and not the whole UI, and it works when I activate the UI manually (it's inactive by default), but when I activate it with the script it doesn't work

    Edit 3: I think I figured it out, I just had a panel blocking the buttons, I didn't realize that happened
     
    Last edited: Nov 28, 2023
  43. Olorin08

    Olorin08

    Joined:
    Sep 15, 2023
    Posts:
    1
    I had same problem and nothing worked

    The issue was I already had event system active before adding the tmp button, and for some reason it was deactivated, so I had to reactivate it and it worked
     
  44. kojagiri

    kojagiri

    Joined:
    Aug 9, 2022
    Posts:
    9
    In my case it was a cursor hotspot issue. I have a crosshair cursor, so the CENTRE of the cursor and not the TOP-LEFT needed to be considered....

    Which I missed after updating the cursor; so the button wasn't activating on-hover in a certain area.
     
  45. mysterygames90

    mysterygames90

    Joined:
    Jan 26, 2022
    Posts:
    1
    Thank you it worked for me just by adding the graphic raycaster to my canvas component so silly it's this hard to use a dang button lol
     
  46. miltinn

    miltinn

    Joined:
    Jan 2, 2023
    Posts:
    1
    So I stumbled upon this problem today, and got it solved with some of the answers here and some elsewhere, so I will list here what I needed to do to make sure the buttons work:

    1. Buttons must be inside the Canvas object hierarchy
    2. Make sure that all buttons have the "Interactable" checkbox marked
    3. Make sure that all the components have the right position values (I'm creating a 2D game, so if the button was behind the main screen, it wouldn't work)
    4. In canvas object, make sure it has the right components: Canvas, Canvas Scaler, Graphic Raycaster and Canvas Group. Some of them were missing for me, so I added them with the default settings (this is one of the main issues because these components were meant to load with the object automatically when you insert a Canvas in your game, so watch out for that).
    5. Make sure you have your EventSystem object in the hierarchy. To check, try to create an object with a right click > UI > Event System. If you have one already, it will be highlighted in the hierarchy.
    6. Inside Event System, you need to have the "Event System" and the "Standalone Input Module" components added, again with default settings.

    These were all the things I had to check to make it work as expected. If you happen to need some extra steps, feel free to update this list when you get it working ;)
     
  47. Kh-coding

    Kh-coding

    Joined:
    Mar 16, 2024
    Posts:
    1
    to fix this problem you can add component -> standard input module
    that fixed the problem for me
     
  48. Fun_and_Games

    Fun_and_Games

    Joined:
    Mar 8, 2023
    Posts:
    3
    FINALLY. I've been stuck on that for a little bit.