Hello, I can I make a int for my coroutine that can check the value of the int every frame.

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;
}

}

I think you just put the ienumerator in the wrong place it has to be after the void. Try like this:

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()
 {
 // Makes the player dash until waitTime is over then reset the player speed
 // 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); 
 }

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