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
0
Question by Neurus_Ex · Nov 06, 2016 at 09:48 PM · mouseclickdetectionmouseclick

Detecting mouse click on GameObject with any mouse button.

I'm a bit embarrassed for asking this, but it seems all the previous question are quite old and not really solve what I'm trying to do.

As simple as it gets: I got a cube with a script attached to it. I need to know when the player clicks the cube, so I can do stuff in a separate function. I'm running some tests, as the last time I touched the input system was some years ago, and each time I click the cube I got 6 output messages (Which I assume correlates to 6 calls to my separate function).

Code:

     private bool[] mousePressed = { false, false, false };

     // Update is called once per frame
     void Update () {
 
         mousePressed[0] = Input.GetMouseButtonDown(0);
         mousePressed[1] = Input.GetMouseButtonDown(1);
         mousePressed[2] = Input.GetMouseButtonDown(2);
 
         doStuff();
     }
     private void doStuff() {
         if (mousePressed[0] || mousePressed[1] || mousePressed[2]) {
             clickTime = Time.time;
             Debug.Log("Click!");
             mousePressed[0] = mousePressed[1] = mousePressed[2] = false;
         }
     }

I know where the problem comes, as Update() is called once per frame, thus I got 4-6 calls to the function (and then to doStuff()) per frame. So I added the line to reset the mousePressed array:

 mousePressed[0] = mousePressed[1] = mousePressed[2] = false;

But it's ignoring me, as I keep getting all the calls per frame.

And I'm clueless. I know I can use

 OnMouseDown()

but it only detects left clicks (and I need both left and right clicks, and also this is somewhat personal now).

So, any help here?

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
Best Answer

Answer by Neurus_Ex · Nov 08, 2016 at 08:34 PM

Well, as pointed by @Eric5h5 on the comments, the problem wasn't on the code itself but in the scene, as I had several cubes with the same script.

Even the line

 mousePressed[0] = mousePressed[1] = mousePressed[2] = false;

was useless, as, obviously, the array was updated in the Update() function.

(P.S.: @Eric5h5, if you update the answer with the several objects thing I can accept it as answer, as that was my problem/question).

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 Eric5h5 · Nov 08, 2016 at 10:33 PM 0
Share

Well...the thing is, the problem was in fact the code itself, not the scene, so this answer isn't really correct. By itself it's not an issue to have the same script running on multiple objects, it's the way you were doing it that was wrong. You said in your question, "I got a cube with a script attached to it. I need to know when the player clicks the cube [both left and right clicks], so I can do stuff in a separate function", and that's what my answer does. Feel free to have multiple cubes with that same script attached, and it works fine.

avatar image Neurus_Ex Eric5h5 · Nov 10, 2016 at 09:06 AM 0
Share

Yeah, and that's why I was asking the question I did. $$anonymous$$y question was not "I got this, I need this", but "I have a problem and I need to solve it." Turned out the problem wasn't about the script but about the scene, thus the question got answered.

So, whatever, dude. Your answer did not answered my question (even less the piece of code without comments), the comments did.

avatar image
6

Answer by Eric5h5 · Nov 07, 2016 at 12:52 AM

 void OnMouseOver () {
     if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2)) {
         Debug.Log ("Click!");
     }
 }
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 Neurus_Ex · Nov 07, 2016 at 08:17 AM 0
Share

Care to explain a bit why does this work?

I can't access right now to my project, but for what the documentation says, this is called each frame the mouse is over the object. Won't I'll get the same result (a click will generate several "Click!" outputs) with an additional set of calls to the function as soon as the mouse gets "above" the object?

avatar image Eric5h5 Neurus_Ex · Nov 07, 2016 at 08:32 AM 0
Share

I don't really follow, sorry. It works because it checks for left/right/middle clicks if the mouse is over the object. The result is essentially the same as On$$anonymous$$ouseDown, except for any button. Nothing else happens.

avatar image Neurus_Ex Eric5h5 · Nov 07, 2016 at 08:56 AM 0
Share

Did you read my whole question? $$anonymous$$y problem comprises that using get$$anonymous$$ouseButtonDown I got 4-6 calls per click (as "I'm clicking" the cube for 4-6 frames).

Show more comments
Show more comments

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

60 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

Related Questions

detecting if the mouse is inside any gui element 1 Answer

Raycast hit not detected on cube 1 Answer

Right mouse detection 2 Answers

Mouse Over & Mouse Click 1 Answer

Unity mouse input gradually becomes off 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