Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by DoctorTwentyThree · Oct 19, 2017 at 07:13 PM · ui2d gamebuttonseventsystem

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoctorTwentyThree · Oct 19, 2017 at 07:54 PM 1
Share

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! :)

avatar image ghostmode DoctorTwentyThree · Oct 19, 2017 at 08:27 PM 0
Share

No worries, glad you got it working

avatar image
2

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;
 }










Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Casiell · Aug 24, 2018 at 07:05 AM 0
Share

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.

avatar image DoctorTwentyThree · Aug 24, 2018 at 07:10 AM 0
Share

Did the Start() fire when the first button was clicked or when the panel got visible?

avatar image oppyJ · Aug 24, 2018 at 12:22 PM 0
Share

@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

avatar image Casiell oppyJ · Aug 25, 2018 at 01:07 PM 1
Share

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

avatar image oppyJ Casiell · Aug 25, 2018 at 06:02 PM 1
Share

Thanks @Casiell , your right :) this is now resolved. Awesome thanks.

I am also going to look into toggle too.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI button not working 1 Answer

Buttons stopped working after I temporarily changed the font. 1 Answer

EventSystem.SetSelectedGameObject Does not display highlight animations for my buttons 1 Answer

Highlight button via script at runtime 5 Answers

EventSystem: how to highlight instantiated button? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges