- Home /
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;
}
}
Answer by Pokedlg3 · Feb 02, 2021 at 03:33 AM
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;
}
}
@Pokedlg3 Yes I am aware of that and I. have changed it but the out come is still the same. If you don't $$anonymous$$d try checking it again for the problem.
Your answer
Follow this Question
Related Questions
Trying to trigger a function on another script 1 time, with an Int involved. PlsHelp 0 Answers
why does my current code line consist next line of code information 1 Answer
Creating a Playerpref score system 1 Answer
SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers
Coroutine? 4 Answers