- Home /
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.
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
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.
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.
Wow I do indeed hate myself right now. That was what was happening. Thank you for the reply!
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
}
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();
}
}
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.