- Home /
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;
}
}
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.
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 :)