Question by
TheBleedingNova · Nov 14, 2015 at 07:22 PM ·
c#inputleveldouble-click
Pressing Same Button Twice
Hi. I'm trying to write a script where I can keep pressing space and I don't know how. I have it set up so when you press space for the first time it makes lastPress = 0. But then it just skips over the middle input,
Text instruction;
string lastPressed;
// Use this for initialization
void Start () {
instruction = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Space)) {
instruction.text = "It is 6:35 am";
lastPressed = "1";
}
if (Input.GetKey (KeyCode.Space) && lastPressed == "1") {
instruction.text = "You must wake up";
}
}
}
Comment
Answer by Onufriyev · Nov 15, 2015 at 04:57 AM
private int lastPressed = 0;
void Update () {
if (Input.GetKey(KeyCode.Space)) {
if (lastPressed != 1){
instruction.text = "It is 6:35 am";
lastPressed = "1";
} else {
instruction.text = "You must wake up";
}
}
}
Thanks @Onufryiyev, you helped come up with this for the solution I needed for my project. Ins$$anonymous$$d I needed lastPressed to be reset after button being pressed twice. Cheers!
private int lastPressed = 0;
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
lastPressed++;
if (lastPressed > 1)
{
// run code
// reset button lastPressed to 0
lastPressed = 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612045026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problems with touch input. 0 Answers
Player follow touch when holding down your finger on the screen (C#) 1 Answer
Moving relative to camera 0 Answers
Pro controller not vibrating 0 Answers
How can i ask a user for their name? 1 Answer