The variable is continuously added to Update ()

I am newbie.

I'm having a very strange problem: the entry I registered is constantly being added to the 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 exclude the output: s Counter down 1 Finish down

However, in the log I got: s Time counter 1 Counter counter 2 counter 3 counter 3 counter 4 counter 5 counter 6 counter 7 counter 8

It's really weird, no matter what I do, I tried counter ++, counter = (counter + 1). Nothing else. After the function, the counter just adds 7 more times, but "Down" and "Finish Down" never showed up a second time. This script is attached to an empty object in an empty project. My editor version is 4.5.4

Any help would be appreciated

+3


source to share


1 answer


I got the result you wanted by adding this change to my code:

else if (Input.GetKeyDown(KeyCode.DownArrow))
{   
    Debug.Log("s Down");

    counter=counter+1;

    Debug.Log (string.Format("counter {0} ",counter));
    Debug.Log("Finish s down");
}

      



Result:

enter image description here

0


source







All Articles