Hello, I can I make coroutine check waitTime every frame instead of reading it as 1.

Currently I have a int called waitTime each time 1 stamina is used it will add on how long the player can dash. My problem is that the coroutine is reading it as 1 instead of checking if the waitTime has increase.

Example: So if I have 10 waitTime that should mean that the coroutine should wait 10 seconds.

How do I do that with my current script?


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerDashScript : MonoBehaviour
{

public playerInfo playerInfo;
public playerGridMovementScript movementScript;
public StamiaBar StaminaBar;

public int waitTime;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    // If key is pressed 1 stamina will be used
    if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f && Input.GetKeyDown(KeyCode.F))
    {
        if(playerInfo.currentStamina >= 1)
        {
            dashStamina(1);
            StartCoroutine(dash());
        }
        else
        {
            StopCoroutine(dash());
        }

    }

    // If key is held down then all of stamina will be used 
    else if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f && Input.GetKey(KeyCode.F))
    {
        if (playerInfo.currentStamina >= 1)
        {
            dashStamina(1);
            StartCoroutine(dash());
        }
        else
        {

        }
    }

}

void dashStamina(int used)
{
    playerInfo.currentStamina -= used;
    waitTime += 1;

    StaminaBar.SetStamina(playerInfo.currentStamina); 
}

// Makes the player dash until waitTime is over then reset the player speed
IEnumerator dash()
{
    movementScript.moveSpeed = 5f;
    yield return new WaitForSeconds(waitTime); 
    movementScript.moveSpeed = 1.5f;
    waitTime = 0;
}

}

@justacode At first glance, don’t you want to pass waitTime to your coroutine?

    IEnumerator dash(int waitTime)
    {
        movementScript.moveSpeed = 5f;
        yield return new WaitForSeconds(waitTime);
        movementScript.moveSpeed = 1.5f;
        waitTime = 0;
    }

`