• 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 Wendek · Nov 01, 2013 at 12:38 PM · c#mouseclick

MouseClick called twice for one click (more like acts as if it was called twice but actually called once)

[Final Edit]Okay if anyone ever gets the same thing, you might have done the same terribly stupid mistake I did : I had actually attached my "UserInput" script (the one that handles clicking and stuff) to two objects : both the player and its hud, which means that in the end everything was called twice. Yes, that easily ranks into my top 10 most stupid bugs in all language combined.

Hello,

It's not the first time I encounter it, but it's the first time it has an impact. Basically in my game the player can "train" peasants in military buildings to create military units (RTS game). The way I do it is that you first send the peasant inside the building, then click on it and train whatever unit you want (the peasant is "transformed" into said unit) Now the problem is that I can 'store' several peasants so when the player clicks to train one... he actually "stores" two. Which means you can get two military units for each peasant, which makes no sense. Now even if there's a quick fix for this particular situation (using a bool instead of an int) I'm still quite intrigued by this and there will probably be situations where this "double-click" will be really annoying.

Also something that baffles me even more : I printed lines for most code blocks in the function. The highest one was only printed once but all the other were printed twice. So this would indicate that the function itself is called once but somehow it "gets read" twice ? Makes no sense to me to be honest but I'm still a beginner.

Here's the code if needed :

 public override void MouseClick (GameObject hitObject, Vector3 hitPoint, Player controller) {
         bool doBase = true;
         Debug.Log ("MouseClick for " + this + " with " + hitObject + hitPoint + controller);
         if(player && player.human && currentlySelected && hitObject && hitObject.name!="Ground")
         {
             Debug.Log ("First if");
             Building building = hitObject.transform.parent.GetComponent<Building>();
             if(building)
             {
                 Debug.Log ("Second if");
                 if(building.UnderConstruction())
                 {
                     SetBuilding(building);
                     doBase = false;
                 }
                 else if (IsTrainingBuilding(building))
                 {
                     Debug.Log ("Passing here for " + this);
                     TrainingBuilding TB = building.transform.parent.GetComponentInChildren<TrainingBuilding>();
                     TB.AllowTraining();
                     CompleteDestroy ();
                     doBase = false; //Shouldn't matter anyway
                 }            
             }
         }
         if(doBase)
              base.MouseClick(hitObject, hitPoint, controller);
 }


Thanks in advance for your help

[Edit]So just to clarify, at runtime the first log appears once and the three others all appear twice. This means that "AllowTraining" is called twice and of course that's where this particular bug comes from.

Comment
Add comment · Show 15
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 Dave-Carlile · Nov 01, 2013 at 12:59 PM 0
Share

Does a NULL get printed in the log for the first message? That would happen if any of the function parameters are null.

What calls your $$anonymous$$ouseClick function? That doesn't appear to be a standard Unity function.

avatar image Wendek · Nov 01, 2013 at 01:18 PM 0
Share

Nono everything is correct and it prints the correct parameters. $$anonymous$$ouseClick is called in "Left$$anonymous$$ouseClick" in UserInput.cs. I followed this tutorial : http://stormtek.wordpress.com/ which I'm now "changing" to fit the needs of my games. It uses a "C&C type" of click where you do most of your stuff with the left mouse button.

If you want it, here's the code for the function that calls it :

 private void Left$$anonymous$$ouseClick() {
         if(player.hud.$$anonymous$$ouseInBounds()) {
             if(player.IsFindingBuildingLocation()) {
                 if(player.CanPlaceBuilding()) player.StartConstruction();
             } else {
                 GameObject hitObject = Work$$anonymous$$anager.FindHitObject(Input.mousePosition);
                 Vector3 hitPoint = Work$$anonymous$$anager.FindHitPoint(Input.mousePosition);
                 if(hitObject && hitPoint != Resource$$anonymous$$anager.InvalidPosition) {
                     if(player.SelectedObject)
                         player.SelectedObject.$$anonymous$$ouseClick(hitObject, hitPoint, player);
                     else if(hitObject.name != "Ground") {
                         WorldObject worldObject = hitObject.transform.parent.GetComponent<WorldObject>();
                         if(worldObject) {
                             //we already know the player has no selected object
                             player.SelectedObject = worldObject;
                             worldObject.SetSelection(true, player.hud.GetPlayingArea());
                         }
                     }
                 }
             }
         }

Basically if the object is already selected, then we call $$anonymous$$ouseClick and this function deter$$anonymous$$es what has to be done for this specific unit. For my peasant's case, it means that when the player clicked on a "training building" (a class I made), then it disappears and is "stocked" inside the building, and unlocks the training of a unit.

P.S. Is it acceptable to post code in comments ?

avatar image Dave-Carlile · Nov 01, 2013 at 01:41 PM 0
Share

Yep, code in comments is fine.

This is definitely an odd one. There's really no possible way the code inside the if can be executed more than once without that first log message appearing too. Is it possible the same messages are being logged elsewhere too? In TB.AllowTraining() for example? Try changing these messages to something different - "First If X", "Second If X", and so on. Do the duplicated messages both have the new text?

If that doesn't reveal anything, I'll usually start breaking things down into smaller pieces, rearranging code if necessary to try to narrow things down. Basically remove stuff until the problem stops happening, then add stuff back until it breaks again. That will usually reveal the issue eventually.

avatar image Dave-Carlile · Nov 01, 2013 at 01:42 PM 0
Share

Also, can you include your log here? That may help.

avatar image Wendek · Nov 01, 2013 at 01:54 PM 0
Share

Here's a screenshot of the log : http://i.imgur.com/PPduF03.png Everything is included except one irrelevant line. The only action done to get this is : select a peon (peasant), click on the building to train it. Note : the "$$anonymous$$ouseClick on Peon" line comes from the superclass log. It appears for the "selection" click, so it is perfectly normal. I should have commented it out before taking the screenshot.

Also I used a screenshot because well I haven't found (nor searched tbh so far) where are the logs actually saved, if they are.

Edit : looks like actually it gets called twice

avatar image Dave-Carlile · Nov 01, 2013 at 01:59 PM 0
Share

Hmm - I though you were saying the first log message ($$anonymous$$ouseClick For...) wasn't displaying the second time? They all look normal to me. So it just appears that indeed $$anonymous$$ouseClick is being called twice...

Or were you referring to the "$$anonymous$$ouseClick on Peon" message missing the second time? The code you've included doesn't show where that message is logged. Can you show that code?

Another question - is $$anonymous$$ouseClick being called for $$anonymous$$ouseDown and again for $$anonymous$$ouseUp maybe?

avatar image Wendek · Nov 01, 2013 at 02:48 PM 0
Share

Actually yes I noticed here that it appears twice. I guess I misread it the first time. I will look into $$anonymous$$ouseUp and $$anonymous$$ouseDown and see if that's the answer, thanks for the idea. The "$$anonymous$$ouseClick on Peon" is inside the superclass of Peon. It's called at the time of the selection, not at the time of the action.

avatar image Dave-Carlile · Nov 01, 2013 at 02:56 PM 0
Share

Another potential issue is if the script is attached to multiple objects. It'd have to be the script that has the "$$anonymous$$ouseClick on Peon", or whatever is calling that...

avatar image Wendek · Nov 01, 2013 at 04:08 PM 0
Share

What do you mean attached to multiple objects ? Peon is a Prefab so the script is attached to the prefab (and thus every instance) but other than that, only Peon units have this script attached to them.

avatar image Dave-Carlile · Nov 01, 2013 at 04:42 PM 0
Share

How are you deter$$anonymous$$ing of someone is clicking on a peon then? Is it possible that two of them are in the same place, so both think they're clicked on? If that were the case then the script would be executed for both - i.e. a single mouse click would result in the script executed for the two units.

avatar image Wendek · Nov 01, 2013 at 06:29 PM 0
Share

There is only one Peon on the map so no. Haven't really had the time today to check in more details this $$anonymous$$ouseUp and $$anonymous$$ouseDown thing, I really hope it's this. It would explain why I "randomly" had two calls for some functions before.

avatar image citizen_rafiq · Nov 04, 2013 at 03:03 PM 0
Share

double attachment of same script's cause this type of problem also, or Override $$anonymous$$ouseClick() method called from two different scripts each frame

avatar image Wendek · Nov 04, 2013 at 03:21 PM 0
Share

Yeah if you read the edit I just discovered that it was double attachment of my script. I'm mad of having spent so much time for such a BS bug but... that's how program$$anonymous$$g works I suppose. ^^

avatar image Dave-Carlile · Nov 04, 2013 at 03:25 PM 0
Share

Frustrating to be sure. But this is how you build up a mental list of things to look for when you have problems like this. It's pretty much the only way to learn program$$anonymous$$g.

avatar image sumit47 · May 11, 2018 at 10:30 AM 0
Share

i am also facing same issue.....do u found any reason behind it?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Editor Script - How to process a mouseclick in the scene? 1 Answer

MouseUp not working on Mobile build 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