- Home /
strange behaviour for coroutine and invoke
something strange is happening and I don't know why. All I'm trying to do is set a bool and a light for 5 seconds. It works the first time its activated but all subsequent times it only stays active for about a second. These are the 2 ways I tried it but had the same problem:
void OnTriggerEnter2D(Collider2D col)
{
invinsable = true;
foreach(Light light in lights)
{
light.enabled = true;
}
Invoke("SetBoolBack", 5f);
}
private void SetBoolBack()
{
invinsable = false;
foreach(Light light in lights)
{
light.enabled = false;
}
}
and here is my coroutine attempt:
void OnTriggerEnter2D(Collider2D col)
{
invinsable = true;
foreach(Light light in lights)
{
light.enabled = true;
}
StartCoroutine(Invincible());
}
IEnumerator Invincible()
{
while(true)
{
yield return new WaitForSeconds(5);
invinsable = false;
foreach(Light light in lights)
{
light.enabled = false;
}
}
}
Comment
Answer by xxmariofer · Mar 16 at 07:10 AM
remove the while true from the coroutine, but the invoke method should be working
IEnumerator Invincible()
{
yield return new WaitForSeconds(5);
invinsable = false;
foreach(Light light in lights)
{
light.enabled = false;
}
}