Hey everyone, I’m new to Unity and I started learning script not long ago, and I’m having a problem with a script, what I’m trying to do is to change a value when I press a key, but this value must change back to its original value after some seconds…
That’s what I’m trying to use:
public float tempoAtaque1;
public float tempoFuncAtaque;
public int defModf;
If you want people to help you in these forums, wrap your posted code around [ code ][ /code ] blocks and try to indent it properly. It helps people to read it more easily and improves your chances of getting help.
I have no idea what your code is actually supposed to be doing, but nevertheless, I believe you had a logical error in the Update function (you forgot to add curly brackets, making defModf always snap to 10 and 0 regardless of the if statement’s validity).
You will notice that I added two new variables. Variable delayTime is how long you want the delay to last before it snaps back to its original value. Variable isButtonPressed is just to force the Attack1 coroutine to only execute once at a time. The command you are looking for the delay itself is WaitForSeconds, which can only execute from within an IEnumerator function (and this is why we can’t put it in Update or your Start overload). We execute IEnumerators through the use of StartCoroutine function instead of calling them directly.