- Home /
Is the Input.GetButtonUp detected twice in update?
Hey everyone, I'm with some pretty noobie question here. What I exaclty wanna know is why the print inside this if statement is showed twice when I stop holding the mouse button?
if(Input.GetButtonUp("Fire1")) {
selectUnits(unitSelecionRect);
print("Event ocurred"); // this is showed twice in the console
foreach(gameUnit u in units) {
u.gameObject.renderer.material.color = Color.red;
}
}
The problem is that I need to add the items that the player was selecting to an array, and because the event occur two times, every item is added two times into the array. Why does this happen?
This only happens once for me using your code. Are you sure you don't have the script attached to two objects?
Yes, it's only attached to the camera. I detect also in update $$anonymous$$ouseDown and $$anonymous$$ouseHold, like:
if(Input.GetButtonDown("Fire1")) {
// do something
}
if(Input.GetButton("Fire1")) {
// do something
}
if(Input.GetButtonUp("Fire1")) {
//execute the code above
}
This can be the problem? Should I use else if?
Answer by Berenger · May 08, 2012 at 04:00 PM
Either the script is attached to several GameObjects, or that code is in OnGUI when it should be in Update.
Bérenger, that's exactly what was happening, the code was in OnGUI! Thanks for the hint!
For the record, OnGUI is executed several times per frame to be able to setup all the layouts properly. If you ever have something that must be in OnGUI but you want it to be executed only once per frame, test if Event.current.type == EventType.Repaint, which is always the last GUI call.
Your answer
Follow this Question
Related Questions
Is it possible to distinguish between mouse and trackpad scroll? 3 Answers
Checking if the Mouse Has a Middle Button 1 Answer
Does anyone know how to determine whether or not a mouse is connected? 0 Answers
I am making a 2d game and I want to make it where to can pick up bricks with your mouse 3 Answers
Detect Selection on EditorGUILayout.SelectableLabel 0 Answers