Ui buttons not working everytime

My buttons always go to the “clicked” colour when pressed, but seems to need to be clicked on a specific spot to start the function attached to it. Which is to load scene 0, the main menu, For the first button.

I have:
a canvas with 3 buttons with racycast target enabled enabled on them
An eventsystem in hierarchy
3 Texts to show different stats, but they are all in the upper left corner

I have searched for similar threads and it seems to be a common problem, but I could not find a working answer in the other threads.

here is a gif of how the button works right now: Imgur: The magic of the Internet
The button haves a “sweet spot” where it works
unfortunately the video capture software does not capture the mouse, but it is there :slight_smile:
A screenshot of the scene is in the attached files.
Anyone of you have an idea why this happens?

You probably want the text objects to be RaycastTarget false, or they might intercept clicks and do nothing.

2 Likes

I tried disabling raycast target on the texts, and problem is still there, like a sweetspot needs to be pressed.

edit* I have deleted what i wrote about the texts being the actual button, because it does not hold true anymore, i was probably just me seeing things…

Since the text is a child of the button, having the raycast on it is fine. This is actually the default behavior of buttons when you add one. Which means something else is wrong with your setup. I can’t really tell from your pictures what else could be wrong. It seems odd the color would change but the code wouldn’t execute. Just make sure you aren’t moving off the button when you are clicking it. You can also select the eventsystem object in your scene and then when you click on the button in the game it will tell you a bunch of info like what you clicked on and such. See if that gives you any idea of what is going on.

1 Like

This is what is written when i select the eventsystem and press the Main Menu button, don’t know why mouse is not showing, welp

edit* while button is being pressed down

I do see lastPointerPress shows the MainMenu button. Odd it’s not working still…

So then the next question is. Has it ever worked on the button? Does it always work when you click on the text? Is it possible your code has something in it that changes how it behaves? I’m not seeing anything wrong so far overall.

Yes it works sometimes, you can see the gif if you trust me :),
I was wrong about the text i think, it does not seem to do anything when i press the text only anymore.
Btw. the buttons on my Main menu in Scene 0 works without problem
hmm, another thread said something about layers, but my canvas and all of it’s children is on the UI layer, so i dont know.

This code is on a gameobject in the hierachy and this is where i pull my functions from.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenuButtons : MonoBehaviour {

    public void StartBornholmLevel()
    {
        SceneManager.LoadScene(0);
    }

    public void StartIslandLevel()
    {
        SceneManager.LoadScene(2);
    }

    public void Quit()
    {
            Application.Quit();
    }

}

Pretty simple code, so that isn’t the issue.

Is there something that might be blocking part of the button, thus keeping the button from being hit in some spots but it works in others?

Honestly, I’m running out of basic troubleshooting questions. You may just want to recreate the buttons.

Edit* Yup, it was just an uncheck box, i unchecked “Lock cursor” and it works. oh boy
Ok, so think i found the culprit, I tried adding a fps controller prefab to my main menu instead of the main camera, and by using that camera the buttons got the problem of not registering the hits.
I then tried adjusting the camera settings to be like the main camera’s but it did not resolve it.
aaaaaaaaaaaaaa

1 Like

@KanonKongen Thanks it was the Lock Cursor deal. like you said others worked just fine.

I had a similar issue just now, with a button only being able to be clicked in a very specific “sweet spot” zone. I wanted to share my finding in case someone finds this thread since it’s very similar, and felt very cryptic.

My issue turned out to be having set a new Default Cursor to a custom cursor, which moved the Cursor Hotspot of my mouse click without my realizing.

I fixed the issue by going back into Project SettingsPlayerCursor Hotspot and meticulously changed the X and Y until my cursor was once again clicking the button as expected in the entire Raycast Target button area.

I had a similar issue just now as well. The problem I had was that some text and other invisible placeholders that were blocking the buttons themselves. I selected everything in my canvas that I didn’t want to be clicked and unchecked “Raycast Target” and it worked. Hope this helps someone.

Turning off unused raycast targets is good practice. TMP text have them on by default hiding behind “Extra Settings”! It also has the added advantage of saving a little bit of performance too. :slight_smile: