Hi folks, thanks to you all for making this community alive.
I’ve been messing around with Coroutines and tried to make my own from reading advice from everywhere and I don’t think this simple thing I want was answered literally anywhere else.
I simply want to start a coroutine with a Seconds timer that:
(example)
On button press (keep pressed)
Timer starts (4 seconds) // something happens
On button Stop pressing
Timer stops and resets.
Then if you press that damn button again. the timer starts again.
Whatever I tried, the timer never stopped from running if I unpressed the button.
I (shamefully) post my code here but let me warn you that I tried so much ways that this may not even be the closest that I get from getting it working.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dropper : MonoBehaviour
{
Coroutine thecoco;
public bool runtimer;
private float time;
private bool wazzo;
void Start()
{
thecoco = StartCoroutine(Timer());
wazzo = (Input.GetKeyDown("space"));
time = 4f;
if (!Input.GetKeyDown("space"))
{
StopCoroutine(thecoco);
}
}
public IEnumerator Timer()
{
{
yield return new WaitForSeconds(time);
this.GetComponent<Rigidbody>().useGravity = true;
}
}
private void Update()
{
if (Input.GetKeyDown("space"))
{
wazzo = true;
}
if (Input.GetKeyUp("space"))
{
wazzo = false;
}
if (wazzo == true)
{
StartCoroutine(Timer());
}
if (wazzo == false)
{
StopCoroutine(thecoco);
}
}
}
Thanks, you guys (gals) are amazing
CFL