click on button by mouse ?

Updated on January 22, 2017 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on January 21, 2017

Hi guys
If I press left mouse button and crosser was on “sleep” button. print a message.

public Button sleep;
 void Update () {
 if (Input.GetMouseButton(0) /* on sleep buton */) {
 Debug.Log (" sleep !! "); }
 }
 }
 

  • Liked by
Reply
0 on January 21, 2017

what?

  • Liked by
Reply
Cancel
1 on January 21, 2017

If clicked on sleep button print a message.

Wise
on January 21, 2017

Ok, and?

Show more replies
  • Liked by
Reply
Cancel
0 on January 21, 2017

and what ? if the specific button “which is the sleep button” clicked, print a message. else don’t print the message.
no need to attach the script file on the button.

  • Liked by
Reply
Cancel
3 on January 21, 2017

I think you mean that your cursor was clicking on a button marked “Sleep”. So to do something from that, you want to create a method like:

public void SleepButton () {
Debug.Log (" sleep !! ");
}

and then, in the inspector of the button, down at the bottom… assign the function.

If you’re having troubles figuring out how to assign buttons, look here for the manual.

on January 21, 2017

Yes, all what you said is correct. But I want use IF statement. Can I do that ?

Devoted
on January 21, 2017

Sure. I think so. Why not?
But, it sounds like you might rather use an event trigger instead!
 
Currently (by default), clicking on a button is only calling on the method or function assigned to it when the mouse button is released (aka: OnMouseUp(0) ). But you can instead have it specialized to be activated during other times (aka events) such as when the user presses down the mouse.

So, instead of an OnClick() assignment, you use an event trigger as tutorialed here
explicitly shown around 4 minutes in. That way you can call on a function when the pointer enters, exits, presses down, clicks off, etc…

This essentially handles an if statement for you so that you wouldn’t have to check if the user is GetMouseUp() or GetMouseDown() yourself.

Feel free to experiment with this! 
Or, if it’s still not what you’re looking for, then I think you’ll have to provide a more thorough explanation, with actual examples, of what you want.

on January 22, 2017

I was thinking on IF statement. But maybe event trigger will solve my issue. I will check it . Thank you Beal.

Show more replies
  • Liked by
Reply
Cancel