• 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

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.

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

An OS design issue: File types associated with their appropriate programs 1 Answer

Turn single script into three seperate actions (mouse clicks) 2 Answers

I am building a missile defense game, i need a code that will fire missiles simultaneously at multiple mouse click positions. 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