Change a value according to time

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;

void Start () {
tempoAtaque1 = 10.0f;
tempoFuncAtaque1 = 10.0f;
}

void Update () {
if(tempoFuncAtaque1 < tempoAtaque1)
tempoFuncAtaque1 += Time.deltaTime;
defModf = 10;

if(tempoFuncAtaque1 >= tempoAtaque1)
tempoFuncAtaque1 = tempoAtaque1;
defModf = 0;

if(Input.GetKeyUp (“1”)){
Attack1();
}
}
void Attack1() {

tempoFuncAtaque1 = 0;

}

By the way, I’m using C#.

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.

As for your question :

public float tempoAtaque1;
public float tempoFuncAtaque;
public int defModf;
public float delayTime;
private bool isButtonPressed;

void Start () {
       delayTime = 5.0f;
       isButtonPressed = false;
       tempoAtaque1 = 10.0f;
       tempoFuncAtaque1 = 10.0f;
}

void Update () {
       
       if(tempoFuncAtaque1 < tempoAtaque1) {
                tempoFuncAtaque1 += Time.deltaTime;
                defModf = 10;
       }

       if(tempoFuncAtaque1 >= tempoAtaque1) {
                tempoFuncAtaque1 = tempoAtaque1;
                defModf = 0;
       }

       if(Input.GetKeyUp ("1")) {
                StartCoroutine(Attack1());
       }
}

IEnumerator Attack1() {
       
       if(!isButtonPressed) {
                    isButtonPressed = true;
                    yield return new WaitForSeconds(delayTime);
                    tempoFuncAtaque1 = 0;
                    isButtonPressed = false;

        }
}

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.

It worked perfectly! Thank you very much!

*Sorry about the lack of the code blocks, I just didn’t know how to do it.