- Home /
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;
}
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...
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
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.
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.)
So if everything is being picked up correctly, what is the specific behavior that is indicating you have a problem?
Your answer
Follow this Question
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