- Home /
coroutine working with keyboard but not with bool
Hi All!
I'm fairly new to Unity and I'm trying to implement a simple 3d shooter.
Yesterday I spent literally all day with a couroutine, trying to make the player respawn after death. Today I started from 0 and made a super simple code and it's working, but when I integrated it with my game code still has problems.
Basically if I use the keyboard to respawn the player, everything works fine, but if I use a boolean it doesn't. I can work around this and make it work in another way, but at this point I would like to know why it has this behaviour since I'm still learning.
So, from my GameManager script I'm calling this coroutine:
void Update() {
if (Input.GetKeyDown(KeyCode.K)) {
playerController.EliminatePlayer();
}
if (!playerController.isAlive) { // Input.GetKeyDown(KeyCode.J)
playerController.gameObject.SetActive(superBool);
StartCoroutine(Hide());
}
}
private IEnumerator Hide() {
playerController.ActivateShield();
for (int i = 0; i < 6; i++) {
superBool = !superBool;
yield return new WaitForSeconds(0.5f);
playerController.gameObject.SetActive(superBool);
}
playerController.gameObject.SetActive(true);
playerController.isAlive = true;
playerController.DeactivateShieldInstant();
superBool = true;
}
and the method in the playerController, EliminatePlayer, is just this one:
public void EliminatePlayer() {
gameObject.SetActive(false);
isAlive = false;
Instantiate(baseExplosion, transform.position, baseExplosion.transform.rotation);
}
Now: if in the Update() I put the commented code (in the second if) works as expected, if I leave the code as it is, the gameObject is blinking very fast, not once every half second as it should. I've been using the isAlive boolean in other script and I never had a problem, so I'm guessing it has something to do with the coroutine.
Any Ideas?
Answer by towkop · Apr 23 at 11:34 AM
private bool hideRunning = false;
void Update() {
if (Input.GetKeyDown(KeyCode.K)) {
playerController.EliminatePlayer();
}
if (!playerController.isAlive&&!hideRunning) { // Input.GetKeyDown(KeyCode.J)
playerController.gameObject.SetActive(superBool);
StartCoroutine(Hide());
}
}
private IEnumerator Hide() {
hideRunning = true;
playerController.ActivateShield();
for (int i = 0; i < 6; i++) {
superBool = !superBool;
yield return new WaitForSeconds(0.5f);
playerController.gameObject.SetActive(superBool);
}
playerController.gameObject.SetActive(true);
playerController.isAlive = true;
playerController.DeactivateShieldInstant();
superBool = true;
hideRunning = false;
}
What the problem might be is that every frame the player is dead you are starting a new Hide() coroutine. If you add a variable storing info if coroutine is running then everything should work as expected
Thank you towkop, is working perfectly!
And now that I know the solution it actually make perfect sense!
Your answer

Follow this Question
Related Questions
problem with using classes and coroutines 1 Answer
Quaternion.Lerp inside a Coroutine 2 Answers
yield return request never returns 2 Answers
Insantiation of a Coroutine Causes multiple clones 0 Answers