Pergunta

Ok I am using Unity C# (MonoDevelop) and I am learning how to pull component variables from other components. Now I understand how to do that, but I am wondering why when I start the game and press the "F" key it prints "Hello I am a cube" and subtracts 1 from CubeTalkPoints at least 3-5 times. I want it to run the code once per key press.

void Update () {

    if(Input.GetKey(KeyCode.F)) 
        C_Talk(0);


}

void C_Talk(int SpellID = 0, int TalkPoint = 1)
{
    CubeData CubeSub = GetComponent<CubeData>();

    if (CubeSub.CubeTalkPoints >= TalkPoint) 
    {
        CubeSub.CubeTalkPoints -= TalkPoint;

        Debug.Log("Hello I am a Cube!");
    }




}
Foi útil?

Solução

Use GetKeyDown() instead of GetKey(). GetKeyDown() will only be true the first frame the button is down. GetKey() will be true every frame as long as the button is held down.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top