Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by ZeroRadius · Sep 15, 2016 at 04:20 AM · buttononclick

Button OnClick() only being called once?

I am working on a game for android. If you touch anywhere on the screen the player moves up and if you release the screen the player goes down. I wanted to add a button that if you pressed it the player would shoot a projectile. I ran into the problem of when you are clicking the button the player still moves up. I figured that I would increase the points of contact required to move the player up if the button is being pressed. I am going to make the button call a method in another script on the player. Right now the method on the player script just runs Debug.Log(). I have it up and running but the problem is the method I have set to run OnClick only runs one time. I click the button over and over and it never runs a second time. I am testing this in the unity game editor not on android but I have my scripts running in the Desktop configuration so that should not be causing a problem.

Comment
Add comment · Show 2
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 aditya · Sep 15, 2016 at 05:18 AM 0
Share

without the script i can't help you much ... but tell us what is happening after that first click on button, i mean is your player moving up on second click

avatar image ZeroRadius · Sep 15, 2016 at 01:24 PM 0
Share

Yes, the player is still going up on the second click. That is the expected behavior and has nothing to do with the button yet. All the button does at the moment is call a method in another class that is attached to the player. The button can be clicked multiple times but the onClick event only fires the first time.

Here is the relevant bits of the code (I don't think it will help you much)

This is the class and method called by the onclick event:

 public class $$anonymous$$ultiTouchFor$$anonymous$$obile : $$anonymous$$onoBehaviour {
     public GameObject Target;
 
     public void Ignore()
     {
         Target.GetComponent<Player$$anonymous$$ovement>().AddIgnored();
     }
 }

This is the method called from the previous code:

 public void AddIgnored()
     {
         Debug.Log("Add Ignored called!");
     }

As you can see the code is very basic at the moment (and it works) which is why I did not post it. I need to figure out how to get the onClick() event of the button to fire every time the button is pressed, that should have nothing to do with the code.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by pernicious · Sep 16, 2016 at 08:51 AM

I had the exact same problem and you are going to hate yourself for this.

The Debug.Log entry on the Console stacks multiple entries with the same message. A small number on the right indicates the number of times the same log entry has occurred, which I didn't notice initially as well.

Comment
Add comment · Show 1 · 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 ZeroRadius · Sep 16, 2016 at 10:53 AM 0
Share

Wow I do indeed hate myself right now. That was what was happening. Thank you for the reply!

avatar image
1

Answer by nj4242 · Sep 15, 2016 at 07:07 AM

Lot of guys in the forums, having issues, with OnClick ( ) which doesn't able to identify Game Objects accrdingly, For specific click on game objects and to identify which object is clicked and what is not necessarily done through onClick( ).

try using :

 void OnMouseDown( )
 {
     //Code
 }
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 ZeroRadius · Sep 15, 2016 at 07:32 PM 0
Share

Thanks for the reply,

I assume this goes on a script that I attach to the button. If so It is not working. Once I remove the onClick and add this ins$$anonymous$$d it does not work at all where as before it worked one time.

 public class $$anonymous$$ultiTouchFor$$anonymous$$obile : $$anonymous$$onoBehaviour {
     public GameObject Target;
 
     public void On$$anonymous$$ouseDown()
     {
         Ignore();
     }
     public void Ignore()
     {
         Target.GetComponent<Player$$anonymous$$ovement>().AddIgnored();
     }
 }

avatar image nj4242 ZeroRadius · Sep 16, 2016 at 05:30 AM 1
Share

Hey @ZeroRadius

It's completely the wrong way you're using the On$$anonymous$$ouseDown( ) First of all, let me tell you what it is, then after I'll show to use it.

Now, On$$anonymous$$ouseDown( ) is a function which will not efficient with Event System and triggers, as in you're case it's a UI Button, And litrally it'll not going to work with it.

This function works with a game object (not an UI object) with a collider attached, This function is not called on objects that belong to Ignore Raycast layer.

Basically, For a working On$$anonymous$$ouseDown() : you need to remove the event triggers from the game object you want to work and attach only the required component, which is sprite renderer, collider and script with this fucntion attached : void On$$anonymous$$ouseDown( )

either you do this, or if you're not comfortable with the function create an empty game object with renderer, collider, and Event trigger and try to use the PointerClick( ) event ins$$anonymous$$d of a UI button's OnClick( ), and make sure your main camera has raycaster attached.

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

6 People are following this question.

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

Related Questions

Unity 5.0.2f1 UI Button OnClick Function 6 Answers

UI Button OnClick Function - How to get name of button that was clicked? 2 Answers

How to begin startcoroutine (IEnumerator) with UI button. 1 Answer

Method added on a button click with a delegate is being called twice. 0 Answers

How to trigger a button click from script 3 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