- Home /
[Solved] (4.6 UI) Button is highlighted without mouse hovering
Hi, I've made a basic pausemenu with the new UI system. But when I open and close a submenu the button clicking doesn't work well any more. I made a gif (yellow=highlighted, red=pressed):
I haven't much code to show, because I mainly used the editor and if I don't use the mouse but a gamepad (which is handled by my code) everything works well. It seems to be a problem with the graphical raycaster or something like this. Any Ideas?
Start the game with $$anonymous$$aximizeOnPlay NOT selected. Highlight those Buttons in the Panel and have it so you can see the scene view and the game view. See if you can spot at what point the rect transform is moving or at least see if we can get some more info.
the rect transform isn't moving and there are no (visible) changes to the buttons. But I recocnized that when you click (right click, because it isn't used for activating something) the highlighting is normal again. Should I try to make a dirty workarount and make a rightclick event after the transition?
Answer by Mmmpies · Mar 01, 2015 at 11:14 AM
Got it, the problem is the canvas accepts buttons from both objects because you want to see both on screen at the same time we have to do something different from normal, normally you'd normally SetActive to false on the canvas you don't want to interact.
So Add A Canvas Group on both the Main and Settings base object:
Set the main so interactable and blocksRaycast are ticked and do the opposite for the settings CanvasGroup:
Now edit your scripts, first Settings:
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class Settings : Button
{
public CanvasGroup SettingsCG;
public CanvasGroup MainCG;
protected override void OnActivated()
{
submenuManager.OpenSubmenu(1);
MainCG.blocksRaycasts = false;
MainCG.interactable = false;
SettingsCG.blocksRaycasts = true;
SettingsCG.interactable = true;
}
}
Then Back:
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class Back : Button
{
public CanvasGroup MainCG;
public CanvasGroup SettingsCG;
protected override void OnActivated()
{
submenuManager.CloseSubmenu(1);
MainCG.blocksRaycasts = true;
MainCG.interactable = true;
SettingsCG.blocksRaycasts = false;
SettingsCG.interactable = false;
}
}
Drag the Objects with the CanvasGroup onto the relevant slot in your field area.
Should solve the problem.
thanks, it worked
EDIT: Now it doesn't seem to work :o When I go back, the first button is selected permanently, when I hover over the second, both are highlighted.
Technically the original problem is solved. This is an additional issue. Looking at now though.
EDIT
That's a complex structure you got there, I think it's holding the value of the previous button clicked somewhere. If you press Esc -> Continue -> Esc -> Settings it Exits as though Continue has been clicked, same for the other way round if you click on Settings and then back it appears to hold the value.
Totally separate issue to the one posted and something in your code holding the last clicked button.
Why not simplify it and use the EventTrigger to display the animation/color change, use onClick to call the function ins$$anonymous$$d of extending the Button function?
EDIT 2
Also I just went back to your original scene without my fix in it and the other issues are there just not a obvious as as the question you asked originally.
EDIT 3
I've been thinking about this and I think the issue is you're extending the button class. If you click the Settings button there's absolutely no way it can do anything other than call the associated script, but because you extend the Button it's possible your code is reacting to the OnClick elsewhere. That's the only way it can happen. I know it's horrible but I think you need to rewrite so you just access public functions to handle clicks and not directly access the OnClick.
I don't derive from the button class, but from my button component (same class name, different namespace), which has an abstract function onClick and I use it for a clean OOP design. Here is a new zip if you need. And it's not held in my code, because it works without a mouse.
I reverted back to your original upload before my script/CanvasGroup got anywhere near that scene. The issues you mention after my fix got applied to it were already there. You press Esc -> Continue -> Esc -> Settings it exits the menu as though Continue had been clicked. That happens in the original upload and in the one you just uploaded. It's got nothing to do with the CanvasPanel Fix I showed you.
Fundamentally your scripts are calling one buttons actions even though a different button is being pressed. Honestly that's a mess, an impressive mess but a mess all the same.
Really don't name a class the same as a Component that way you can avoid confusion but there are far easier ways of doing this. The event system already handles clicks and is robust and very, very good. Is there a reason for the complexity?
Currently the only part that's working as expected is the CanvasGroup fix.
EDIT
Well there's shouldn't and and what's actually happening, try what I suggested Esc -> Continue -> Esc -> Settings if it exits the menu then Settings is being clicked and Continue is being called, that's what's actually happening.
$$anonymous$$y buttons aren't calling other buttons than pressed (not intended at least). O$$anonymous$$, the name could be different, but that shouldn't produce this bug. I did that system for an easy gamepad integration.
Your answer
![](https://koobas.hobune.stream/wayback/20220613184520im_/https://answers.unity.com/themes/thub/images/avi.jpg)