The question is answered, right answer was accepted
Why is my coroutine not working?
I'm trying to set a canvas (it's disabled when the game starts) to true after a player dies, but like 5 seconds after the player has died. I'm trying to use a coroutine and an Ienumerator but it keeps the canvas disabled. This is what I have:
IEnumerator endSceneActive()
{
end.transform.position = new Vector3(0,1, zPosofDeadPlayer + 6);
yield return new WaitForSeconds(5);
end.SetActive(true);
}
void PlayerisDead()
{
StartCoroutine(endSceneActive());
}
Answer by victorbisaev · Jan 31, 2018 at 12:38 AM
You can try to do the following:
Check if the Canvas is enabled in Hierarchy panel in editor after 5 seconds.
Add "Debug.Log" before and after "yield return new WaitForSeconds(5);" and check if the messages appear in Console panel.
Add "Debug.Log" just prior to "StartCoroutine(endSceneActive());" and check Console message
The code you provided looks good so may be the problem is not in coroutine.
I can't add Debug.Log because of this error "Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement"
Try this:
IEnumerator endSceneActive()
{
Debug.Log("endSceneActive before yield");
m_chunks.transform.position = new Vector3(0, 1, 3 + 6);
yield return new WaitForSeconds(5);
Debug.Log("endSceneActive after yield");
m_chunks.SetActive(true);
}
void PlayerisDead()
{
Debug.Log("PlayerisDead before StartCoroutine");
StartCoroutine(endSceneActive());
}
If you see all the debug messages in your Log panel then check zPosofDeadPlayer
value - maybe it is absolutely wrong.
Also please notice that end.transform.position
set absolute world position. Check if the Canvas is in the camera frustum when it is at this world position.
I removed the zPosofDeadPlayer and I changed it so there would be a camera being enabled and disabled, but it still doesn't work. The log doesn't show anything. It seems like the IEnumerator is just not activating at all, the function doesn't seem to be going through.
Edit: If the player dies, would the IEnumerator still start if the PlayerisDead function is called beforehand?
This is how I am calling the function: void OnTriggerEnter (Collider other) { if (other.gameObject.tag == "Floor") { return; }
if (other.gameObject.tag != "Floor") {
PlayerisDead();
Destroy (gameObject);
}
}