- Home /
Variable continuously add up in Update()
Hi All, I'm a newbie.
I'm having a really odd problem here, the filed I defined just continuously add up in Update() function.
This is my code:
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour {
private int counter = 0;
void Update ()
{
ShowSelectedMenuItem ();
}
private void ShowSelectedMenuItem() {
if (Input.GetAxisRaw ("Vertical") > 0.9) {
} else if (Input.GetAxisRaw ("Vertical") < - 0.9) {
Debug.Log("s Down");
counter=counter+1;
Debug.Log (string.Format("counter {0} ",counter));
Debug.Log("Finish s down");
}
}
}
I'm excepting output like these: s Down counter 1 Finish s down
However, In log I got: s Down counter 1 Finish s down counter 2 counter 3 counter 4 counter 5 counter 6 counter 7 counter 8
This is really odd, No matter what I do, I have tried counter++, counter = (counter+1). Makes no different. After the function, counter just keep add up another 7 times but "s Down" and "Finish s down" never showed second time. This script attached to an empty object in a empty project. My editor version is 4.5.4
Any help would be appreciated
Answer by Bunny83 · Oct 13, 2014 at 02:16 AM
You have pressed the "collapse" button at the top of your console window. If that button is toggled on the console will collapse / group the same message into one. That's why you don't see the other logs, bug they are there.
Thanks a lot! How can I make the function occur only once for each key press?
Uhm, i'm not sure what you mean. You're using GetAxisRaw which returns the state of the virtual axis. If you want to check for a single keypress, you have to use either Input.GetButtonDown if you want to use virtual buttons (defined in the input manager), or Input.Get$$anonymous$$eyDown with a $$anonymous$$eyCode of a specific key.
if (Input.GetButtonDown("NameOfAButtonInTheInput$$anonymous$$anager"))
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
I guess that's all you want. If not, be more specific ;)
Your answer
Follow this Question
Related Questions
Why am I losing some inputs on Update function? 0 Answers
How to Count an Input, key press on keyboard 1 Answer
Eliminating input loss 1 Answer
How can I get user-defined inputs? 1 Answer
Input.GetKey not working 1 Answer