Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by grka · Feb 02, 2015 at 12:56 AM · onclicklistener

Button onClick is called when added to the Listener

I try in C# Script too add a method to a Button for the onClick Event. I do this in OnMouseDown of a script that is connected to my gameobject like this:

 Button FeedButton = GameObject.Find("/Canvas/MyButton").GetComponent<Button>();
 FeedButton.onClick.RemoveAllListeners();
 FeedButton.onClick.AddListener(() => { ClickedButton(); });

So far it works but there is one probem: ClickedButton() is called in the moment when I add it to the listener even though the button wasn't clicked that moment...How can this happen?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tourist · Feb 03, 2015 at 04:54 PM

The game object you are talking about is the one the button is in? If so ClickedButton is called because the event is sent on mouse released.

You should either : - change the moment the callback is added. - invalidate the first call using a boolean.

I think the 1st proposition is the right one. I don't know what you are tring to do but this seems ill-designed.

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 grka · Feb 03, 2015 at 06:21 PM

I found a solution by now. The problem was that when I click on my gameobject I move the canvas into the cameraview in the onmousedown method. when I release the mouse button the canvas was already displayed and the mouse click event was sent to the button. I think this is a bug in unity. I made a workaround that I wait a second before I the AddListener

 void OnMouseDown()
 {
    MovingCanvasInView();
    StartCoroutine(AddListener());
 }
 
 IEnumerator AddListener()
 {
     yield return new WaitForSeconds(1);
 
     FeedButton.onClick.AddListener(FeedNow);
 
     yield break;
 }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ArgumentOutOfRangeException 1 Answer

Weird behaviour when trying to dynamically create buttons 0 Answers

button.onClick.AddListener(method); NOT Working 1 Answer

AddListener() Javascript 2 Answers

onClick listener not being added properly via script, but no errors 0 Answers


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