- Home /
WaitForSeconds causing function to return
I am calling WaitForSeconds in a coroutine like this:
public override IEnumerator Activate()
{
if (!playerAttributes.isBurned)
{
spriteRenderer = playerAttributes.GetComponent<SpriteRenderer>();
StartCoroutine(ActiveTimer());
// Mark the player as burned so burn can't be applied again while
// this instance of burn is active;
playerAttributes.isBurned = true;
// Make character red while they are burned
playerAttributes.GetComponent<SpriteRenderer>().color = Color.red;
while (isActive)
{
if (!playerAttributes.isDead)
{
playerAttributes.currentHealth -= damage;
Debug.Log("Activate");
yield return new WaitForSeconds(applyInterval);
Debug.Log("TEST");
}
else
{
isActive = false;
}
}
// White returns the sprite to its original colors
spriteRenderer.color = Color.white;
playerAttributes.isBurned = false;
}
yield return null;
}
this function is called like this:
public void AttemptToInflict()
{
// Inflict status if RNG decides it should happen
if (percentageChanceToInflict >= Random.Range(0, 100))
{
StartCoroutine(Activate());
}
}
This is the contents of ActiveTimer(), statusDuration is 3f:
protected IEnumerator ActiveTimer()
{
isActive = true;
yield return new WaitForSeconds(statusDuration);
isActive = false;
}
"Activate" prints out every time, but "TEST" never prints. applyInterval is set to 1 and is of type float. Why is the function exiting on the WaitForSeconds() call?
Answer by Vega4Life · Dec 22, 2018 at 11:52 PM
It should work... Is there anything that is destroying or disabling that gameObject?
You know what... I think that might be it. This is in a Status class that is a component on a Bullet. The bullet gets destroyed when it makes contact with a player. I will go inspect this...
it's the only thing that makes sense. Code looks legit. :)
$$anonymous$$oving the Status script onto the player fixed the problem! Thank you!