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 Nebula_Coder · Nov 03, 2014 at 02:31 PM · mousenot workingclickgetmousebuttondown

Inconsistent Input.GetMouseButtonDown Detection

I am creating a city building type game. The player can build certain buildings and then these building produce a certain resource or currency over time. When the player then clicks on said building, the resources it has produced are then added to their total amount of that resource.

However, the Input.GetMouseButtonDown is acting a little strange. For the most part, it simply doesn't detect the mouse being clicked and if I frantically click the all over the object over and over it might detect the click once or twice. I have a boolean variable called MouseIsOver that detects whether I'm on the object or not.

Does anyone know what might be wrong here or have a suitable solution? I have been scratching my head about this for quite some time and gotten nowhere. Thank you.

Here is my code:

 if((mouseIsOver == true) && (Input.GetMouseButtonDown(0))){
     if (buildingID == 0){
         Debug.Log("Pressed left click");
         amounts.stone += resourceIncrement;
     } else if (buildingID == 1) {
         Debug.Log("Pressed left click.");
         amounts.water += resourceIncrement;
     } else if (buildingID == 2) {
         Debug.Log("Pressed left click.");
         amounts.wood += resourceIncrement;
     } else if (buildingID == 3) {
         Debug.Log("Pressed left click.");
         amounts.populationCurrent += resourceIncrement;
     }
     resourceIncrement = 0;
 }
Comment
Add comment · Show 8
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 robertbu · Nov 03, 2014 at 02:32 PM 0
Share

We need to see the rest of your script. 'mouseIsOver' might be the problem ins$$anonymous$$d of Get$$anonymous$$ouseButtonDown(), or you might be trying to call this code in FixedUpdate() or...

avatar image Nebula_Coder · Nov 03, 2014 at 02:43 PM 0
Share

Sorry about that. I'll show that to you know.

 public int buildingID;
 public bool mouseIsOver;
 private int resourceIncrement = 0;
 
     void Update () {
 
 
         if(Time.time > nextTime){
 
             resourceIncrement += buildingObject.resourcePerHour/3600;
 
 
             if((mouseIsOver == true) && (Input.Get$$anonymous$$ouseButtonDown(0))){
                 if (buildingID == 0){
                     Debug.Log("Pressed left click");
                     amounts.stone += resourceIncrement;
                 } else if (buildingID == 1) {
                     Debug.Log("Pressed left click.");
                     amounts.water += resourceIncrement;
                 } else if (buildingID == 2) {
                     Debug.Log("Pressed left click.");
                     amounts.wood += resourceIncrement;
                 } else if (buildingID == 3) {
                     Debug.Log("Pressed left click.");
                     amounts.populationCurrent += resourceIncrement;
                 }
                 resourceIncrement = 0;
             }
 
             nextTime++;
         }
 
     }
 
     void On$$anonymous$$ouseEnter(){
         mouseIsOver = true;
         Debug.Log ("Enter");
     }
 
     void On$$anonymous$$ouseExit(){
         mouseIsOver = false;
         Debug.Log ("Exit");
     }

This is all the relevant code. Thanks

avatar image robertbu · Nov 03, 2014 at 03:29 PM 0
Share

You've assumed it is the Get$$anonymous$$ouseButtonDown() that is failing, but looking at the logic, you would have problems with:

  • mouseIsOver not being true

  • nexTime not being greater than Time.time

  • buildingID being outside the range of 0 to 3

It could also be a collider issue. On$$anonymous$$ouseEnter() will only fire if the collider is the front most one. Put Debug.Log() statement at line line 6:

 bool is$$anonymous$$ouseDown = Input.Get$$anonymous$$ouseButtonDown(0);
 Debug.Log("-->"+Time.time+","+nextTime+","+is$$anonymous$$ouseDown+","+buildingID);

Note that Get$$anonymous$$ouseButtonDown() will only be true for a single frame, so mouseIsOver will need to be true when the button is pressed.

avatar image Nebula_Coder · Nov 03, 2014 at 04:26 PM 0
Share
  • mouseIsver is returning as true when it should be (when the mouse enters the collider)

  • nextTime is always greater than Time.time (it is set as nextTime = Time.time + 1 in the Start())

  • buildingIDs have only been assigned within that range

  • Every time the mouse button is being clicked, it is being picked up (I deduced this by inserting the code you recommended.)

avatar image robertbu · Nov 03, 2014 at 04:39 PM 0
Share

So if everything is being picked up correctly, what is the specific behavior that is indicating you have a problem?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem getting my model to animate once mouse clicked 0 Answers

Click and Drag Camera 3 Answers

Can't click on Input fields. 3 Answers

Charging up the speed of an game object through a mouse click 3 Answers

Can't shoot towards mouse click point 1 Answer


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