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 RumplyThrower · Jul 05, 2014 at 07:54 PM · mousedetectiondoubleclicks

GetMouseButtonDown detected twice

This code detects GetMouseButtonDown twice on just 1 click. I created another code to debug it and it seems that only in this code does the bug appear: var statsManager : GameObject; var tower : GameObject; var dummyTower : GameObject; var clone : GameObject; var hitPoint : Vector3; var waiting : boolean = false; var end : boolean = false; var mask : LayerMask;

 var debugCnt : int = 0;
 var shouldAdd : boolean = false;
 
 var normalCostWood = 10;
 var normalCostStone = 20;
 var normalCostFire = 0;
 
 var fireCostWood = 20;
 var fireCostStone = 5;
 var fireCostFire = 2;
 
 function Start()
 {
     end = false;
     while(true)
     {
         if(end)
         {
             if(clone != null)
             {
                 Destroy(clone);
             }
             break;
         }
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 1000, mask)) 
         {
             hitPoint = hit.point;
             if(!end)
             {
                 clone.transform.position = Vector3(hitPoint.x, 20, hitPoint.z);
             }
             if(!clone.GetComponent(canPlace).available)
             {
                 if(Input.GetMouseButtonDown(0))
                 {    
                     debugCnt++;
                     Debug.Log(debugCnt);
                     var wood = statsManager.GetComponent(stats).wood;
                     var stone = statsManager.GetComponent(stats).stone;
                     var fire = statsManager.GetComponent(stats).fire;
                     if(tower.transform.name == "normalTower")
                     {
                         if(wood >= normalCostWood && stone >= normalCostStone && fire >= normalCostFire)
                         {
                             statsManager.GetComponent(stats).wood -= normalCostWood;
                             statsManager.GetComponent(stats).stone -= normalCostStone;
                             statsManager.GetComponent(stats).fire -= normalCostFire;
                             
                             debugClone = Instantiate(tower, Vector3(hitPoint.x, 20, hitPoint.z), Quaternion.identity);
                             debugClone.GetComponent(towerHP).statsManager = statsManager;
                         }
                     }
                     if(tower.transform.name == "fireTower")
                     {
                         if(wood >= fireCostWood && stone >= fireCostStone && fire >= fireCostFire)
                         {
                             statsManager.GetComponent(stats).wood -= fireCostWood;
                             statsManager.GetComponent(stats).stone -= fireCostStone;
                             statsManager.GetComponent(stats).fire -= fireCostFire;
                             debugClone = Instantiate(tower, Vector3(hitPoint.x, 20, hitPoint.z), Quaternion.identity);
                             debugClone.GetComponent(towerHP).statsManager = statsManager;                    
                         }
                     }
                 }
             }
         }
         yield;
     }
 }
 
 function OnEnable()
 {
     clone = Instantiate(dummyTower, Vector3(0,0,0), Quaternion.identity);
     Start();
 }

And here is the simple debug code that works just fine:

 function Start()
 {
     while(true)
     {
         if(Input.GetMouseButtonDown(0))
         {
             Debug.Log("asd");
         }
         yield;
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by WhoRainZone1 · Jul 05, 2014 at 09:40 PM

Heho,

guessing by your variable names, I assume you've running more than one of that scripts at the same time, thus making all instanced throwing the MouseDown event. To only call the MouseDown event on one particular object, make a raycast on click and compare the hit.collider.name with the GameObject's gameObject.name property.

Hope that helps

edit: Yeah, after reading over it again I see you have to change the code this way:

 var debugCnt : int = 0;
 var shouldAdd : boolean = false;
 
 var normalCostWood = 10;
 var normalCostStone = 20;
 var normalCostFire = 0;
 
 var fireCostWood = 20;
 var fireCostStone = 5;
 var fireCostFire = 2;
 
 function Start()
 {
     end = false;
     while(true)
     {
         if(end)
         {
             if(clone != null)
             {
                 Destroy(clone);
             }
             break;
         }
         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast (ray, hit, 1000, mask))
         {
             hitPoint = hit.point;
             if(!end)
             {
                 clone.transform.position = Vector3(hitPoint.x, 20, hitPoint.z);
             }
             if(!clone.GetComponent(canPlace).available)
             {
                 if(tower.transform.name == "TowerID")
                 {
                     if(Input.GetMouseButtonDown(0))
                     {
                         debugCnt++;
                         Debug.Log(debugCnt);
                         var wood = statsManager.GetComponent(stats).wood;
                         var stone = statsManager.GetComponent(stats).stone;
                         var fire = statsManager.GetComponent(stats).fire;
                         if(tower.transform.tag == "normalTower")
                         {
                             if(wood >= normalCostWood && stone >= normalCostStone && fire >= normalCostFire)
                             {
                                 statsManager.GetComponent(stats).wood -= normalCostWood;
                                 statsManager.GetComponent(stats).stone -= normalCostStone;
                                 statsManager.GetComponent(stats).fire -= normalCostFire;
                                 
                                 debugClone = Instantiate(tower, Vector3(hitPoint.x, 20, hitPoint.z), Quaternion.identity);
                                 debugClone.GetComponent(towerHP).statsManager = statsManager;
                             }
                         }
                         if(tower.transform.tag == "fireTower")
                         {
                             if(wood >= fireCostWood && stone >= fireCostStone && fire >= fireCostFire)
                             {
                                 statsManager.GetComponent(stats).wood -= fireCostWood;
                                 statsManager.GetComponent(stats).stone -= fireCostStone;
                                 statsManager.GetComponent(stats).fire -= fireCostFire;
                                 debugClone = Instantiate(tower, Vector3(hitPoint.x, 20, hitPoint.z), Quaternion.identity);
                                 debugClone.GetComponent(towerHP).statsManager = statsManager;
                             }
                         }
                     }
                 }
             }
         }
         yield;
     }
 }
 
 function OnEnable()
 {
     clone = Instantiate(dummyTower, Vector3(0,0,0), Quaternion.identity);
     Start();
 }

In this code, you have to make the tower differntiation by using of tags, as the you have to assign a unique name to each tower when instantiating to compare it to the hit target.

Comment
Add comment · Show 1 · 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 RumplyThrower · Jul 06, 2014 at 10:17 AM 0
Share

It wasn't that, i figured that when i enabled the script for the first time, there were 2 start() functions running at the same time. One inherently and one called by OnEnable() but thanks anyway :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to detect in which half of the screen the cursor is, no matter the resolution? 1 Answer

detecting if the mouse is inside any gui element 1 Answer

How to detect double and single clicks using javascript? 1 Answer

More Accurracy on Mouse Click Detection. 0 Answers

Detect mouse movement 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