- Home /
Why do I have to double click on a button the first time?
I'm making an app with Unity in 2D. I have a button on my canvas, but every time you start that scene you have you click on the button twice to fire the OnClick(), but after that you can click the first time, until you restart the scene, then it's the same thing again. It's just this scene that is affected.
Thanks for answers!
Answer by ghostmode · Oct 19, 2017 at 07:25 PM
The first click might just be giving the game view 'focus', then the next click is received by the game. Try this: Start the game. Click inside the game view but not on the button itself. Then click the button. Did the button work?
This did not work, though you got me thinking of adding another button and some $$anonymous$$utes later I found that the script that the OnClick() fires was inactivated until you pressed the button and then, the script got it Start() (which I thought it got at the start of the game) and disables the panel that the button were supposed to show. Thanks for the help! :)
Answer by AnomMahartha · Oct 07, 2019 at 11:14 AM
if your script like this (Pause Script Example).
void Start()
{
}
void Update()
{
if (GameIsPaused)
{
Resume();
}
else
{
Paused();
}
void Resume()
{
GameIsPaused = false;
//your code
}
void Resume()
{
GameIsPaused = true;
//your code
}
}
You should add GameIsPaused = false on void Start to avoid double click. like this
void Start()
{
GameIsPaused = false;
}
Answer by oppyJ · Aug 24, 2018 at 06:51 AM
@DoctorTwentyThree & @ghostmode I am having this same problem. i have a panel with a menu panel on, in a layout manager holding 6 buttons. When the button is clicked the user is selecting a menu item i.e. one displays a pop up window which works ok, but another onclick() also changes the image of the button to a pressed image so the user can better visually tell the diff between pressed and not, also for design it looks nicer. But when the scene is loaded the sequence of changing the button image on/off images needs 2 clicks to get working.
I have printed a debug.log statement in the Start() of the scrips that are in use. The 2 scripts Start() do get called b4 the button is clicked. Any thoughts?
Thanks
another onclick() also changes the image of the button to a pressed image
You may want to look into Toggle component, it may help you with this button.
You can also share code for this button. I suspect what your problem may be, but I need to see the code.
Did the Start() fire when the first button was clicked or when the panel got visible?
@Casiell what do u suspect?
Not able to get code just now, but what the script has is:
class vars: public Sprite on, off (image resources) private bool isOn (keep track of on off state) Button pressedButton
onclick calls a method to change the image of pressedButton depending on isOn or not (if need the on or off Sprite) pressedButton.image.overrideSprite = on; Also updates the isOn also. There is a 2nd onclick that also fires up, to load a popUp (using a panel to overlay the current panels in view).
@DoctorTwentyThree they fire up once the scene loads. Scene is a panel with background image and panel with menu buttons. all visible on scene load/ play in unity editor.
cheers
I think that maybe your default isOn is false, and the default sprite is onSprite. So in this case when you press the button you change isOn to true, but the sprite stays the same because it was already in that state
Thanks @Casiell , your right :) this is now resolved. Awesome thanks.
I am also going to look into toggle too.
Answer by bblbex2001 · Jan 26, 2019 at 08:26 PM
For people with same problem. Try to move all your UI controlling things affected by button into independent game object. For example, my pause button was need 2 clicks before I move my call pause menu function into independent game object with new script. P.S. Sorry for bad english maybe