So I want an object in my game to be deleted when the player touches it but instantiate again a few seconds after. This is my current code: Code (CSharp): public class JumpDot : MonoBehaviour { private Player player; void Start() { player = FindObjectOfType <Player>(); } void OnTriggerEnter2D(Collider2D other) { if (other.name == "Player") { StartCoroutine("waitThreeSeconds"); player.currentJumps++; player.canJump = true; Destroy(gameObject); } } IEnumerator waitThreeSeconds() { yield return new WaitForSeconds(3); print("waited 3 seconds"); } } So in theory should this not be starting the three second coroutine when the dot is triggered and then printing the message correctly? I'm not seeing anything wrong with this code but I'm sure I'm just being stupid.
you should insert your code like this for better lisibility Code (CSharp): public class JumpDot : MonoBehaviour { private Player player; void Start() { player = FindObjectOfType <Player>(); } void OnTriggerEnter2D(Collider2D other) { if (other.name == "Player") { StartCoroutine("waitThreeSeconds"); player.currentJumps++; player.canJump = true; Destroy(gameObject); }} IEnumerator waitThreeSeconds() { yield return new WaitForSeconds(3); print("waited 3 seconds"); }}
From the looks of it, the coroutine is being started, but then the gameobject is being destroyed. As the coroutine is being run as part of the gameobject, the coroutine will get stopped as part of the gameobject destruction.