D
D
Deathgar2016-11-24 12:27:55
Unity
Deathgar, 2016-11-24 12:27:55

When you click on the button several times, the action is performed, the details are inside. What to change?

Simple script:

public class Rand : MonoBehaviour {
    int dawn;
    public SpawnBox sB;
    void Start()
    { }		
  void Update () {
      if(Input.GetKey(KeyCode.Q))
        {
            dawn = Random.Range(0, 10);
            if(dawn == 3)
            {
                sB.Spawn();
            }
            else
            {
                print(dawn);
            }                
        }
  }
}

When you click on Q, instead of one random number, we get several:
1ca7d148cf83411b9bc3b0f522eb3438.png
How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2016-11-24
@Deathgar

GetKey returns true if the button is in the pressed state, and not the fact that the button was pressed (a la the button was pressed.). If you need to catch a single click, use GetKeyDown.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question