Hello, my probleme is that i have a script called Distance manager which must display the distance done by the player, but my goal is to add +1 to a variable called “distance” when i press “Z”. The probleme is that when i press “Z”, instead of adding only 1 per second, the distance add +1, 60 times a second so i would like to fix this as soon as possible. Ty to the people who will help me
Here is my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DistanceMangerV2 : MonoBehaviour
{
public Text distanceMoved;
public Text marketDistance;
public float distance;
[HideInInspector] public int boughtDistance;
void Update()
{
if (Input.GetKey(KeyCode.Z))
{
StartCoroutine("DistanceCounter");
}
distanceMoved.text = (distance + boughtDistance).ToString();
marketDistance.text = (distance + boughtDistance).ToString();
}
IEnumerator DistanceCounter()
{
distance++;
yield return new WaitForSeconds(1f);
}
}