WaitForSeconds not working in C#
I am making an FPS that's set on the cellular level with pathogens and the immune system, and instead of shooting bullets, the immunities shoot antibodies. I've set up my code so that if a player is hit, they show their damage by changing color to red momentarily, then back to their original color. I've tried to implement a coroutine and call a WaitForSeconds from there, but it's still not working. I've read through articles for at least an hour, yet I can't find why the delay doesn't occur. If you know why this is happening, please tell me. Thanks in advance!
public void DealDamage(GameObject target,int amount)
{
//deal the damage
this.health -= amount;
//make the player change color to red
target.GetComponent<Renderer>().material.color = Color.red;
//***THIS IS WHERE COROUTINE IS STARTED***
//wait for 0.5 secs
StartCoroutine(Wait(0.5f));
//switch back to original color
if (target.tag == "Immunity")
{
target.GetComponent<Renderer>().material = immunityMaterial;
}
else
{
target.GetComponent<Renderer>().material = pathogenMaterial;
}
//if dead, die & respawn
if (this.health < 0)
{
Debug.Log(target.name + " died.");
Destroy(thisPlayer);
Respawn(5);
}
//***THIS IS THE COROUTINE***
IEnumerator Wait(float duration)
{
yield return new WaitForSeconds(duration); //Wait
}
P.S. - The whole public class
is derived from NetworkBehaviour
. Maybe that's the problem?
Answer by Addyarb · Nov 02, 2015 at 06:37 PM
Coroutines were the single most difficult thing for me to understand getting started, so I hope I can relieve some of the frustration.
StartCoroutine() does not make your code pause, it simply starts a method that has the ability to pause - but ONLY in that method.
The way you've got it commented out makes sense, but it doesn't work like that. You have to put the code you wish to separate inside the coroutine. Like so:
float health;
Transform Player,target;
public Material immunityMaterial,pathogenMaterial;
void Respawn(){
//Do respawn
}
void DealDamage(GameObject target, int amount){
StartCoroutine (DamageEffect(target,amount));
}
IEnumerator DamageEffect(GameObject target, int amount){
this.health-=amount;
target.GetComponent<Renderer>().material.color = Color.red;
yield return new WaitForSeconds(0.5f);
if (target.tag == "Immunity")
target.GetComponent<Renderer>().material = immunityMaterial;
else
target.GetComponent<Renderer>().material = pathogenMaterial;
if (this.health < 0) //if dead, die & respawn
{
Debug.Log(target.name + " died.");
Respawn(5);
Destroy(this.Player);
}
P.S. I moved the Destroy(this.Player) to after the Respawn(5) because once you destroy your object, you also destroy this instance of code - meaning Destroy() better be the last thing you call, if it's destroying the object that this script is on. There's a way around that - by using a delay. Destroy(this.Player,5) waits 5 seconds and then destroys the Player, for instance.
But I digress...
If you're like me, you're wondering at this point "That seems really redundant just to get a 0.5f second gap inbetween my code. Well that's C# for ya!
Only call a coroutine on the actual object the IEnumerator is on. The coroutine itself resides on the caller, not the recipient. As long as they're both the same thing, everything is fine. However, if you were to call a coroutine on something, and then you got destroyed, you would halt the coroutine on that thing indefinitely.
For clarification, see the thread I learned about it the hard way!
Good luck with your game!
Yeah, the Destroy()
and Respawn()
thing I just threw in to show what I was gonna do there, didn't actually code that yet :) Thanks for the help, just modified your code so that Cmd_Shoot()
runs the DealDamage()
coroutine straight from inside it. Thanks again!
Hey, sorry about not accepting your answer as the best one, kinda forgot :(
Your answer
Follow this Question
Related Questions
Coroutine working in Play Mode, but not on build 0 Answers
c# Coroutines and Waypoints HELP PLS!!!,C# Coroutine and Waypoints Help pls!!! 2 Answers
Why isn't my coroutine working when I call it from another script. 0 Answers
Wait time after coroutine's wait seconds is complete 0 Answers
Coroutine WaitForSeconds ignoring StopAllCoroutines... How can I do it? 0 Answers