I need to make my death function wait a few seconds before teleporting my character to the spawn point, how do I implement co-routine?
So I've seen tons of things for the WaitForSeconds function with coroutines but I just can't figure out how to apply them here because even with the scripting API and all the questions on here I can't figure it out. I'm trying to make it so when my character dies their meshrenderer is turned off and they instantiate a particle effect before finally changing the characters transform to equal an empty GO called "spawnPoint". I have this working but I want there to be 2 second delay between when the meshrenderer is turned off the the teleportation so you can see the particle effect before starting over but I can't figure out how to make this work. (quick side note: I know my code is probably hideous at the moment but I just want to make this work before I fix other issues) Here's the code I've written so far:
 using UnityEngine;
 using System.Collections;
 
 public class PlayerMoveScript : MonoBehaviour {
     public float hor;
     public float speed;
     public float jumpSpeed;
     public bool canJump = true;
     public Transform spawnPoint;
     public GameObject deathParticle;
     public Rigidbody rb;
     public GameObject cubeScoot;
     public GameObject cylinderScoot;
     
     
     void Start () {
         rb = GetComponent<Rigidbody>();
     }
     
 
     void Update () {
       
         hor = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         //ver = Input.GetAxis("Vertical") * speed * Time.deltaTime;
         transform.position += new Vector3(hor, 0, 0);
     }
 
     void FixedUpdate() {
         if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
         {
             rb.AddRelativeForce(Vector3.up * jumpSpeed);
             canJump = false;
           //  if (transform.position.y >= transform.position.y + 3.5f) {
           //  rb.AddRelativeForce(Vector3.down * jumpSpeed);
           //  }
         }
     }
 
     void OnCollisionEnter(Collision other) {
         if(other.gameObject.CompareTag("floor"))
             canJump = true;
     }
 
     void OnTriggerEnter(Collider trigger) {
         if (trigger.gameObject.CompareTag("Trap")){
             PlayerDeath();
         }
     }
 
     IEnumerator PlayerDeath(){
         cubeScoot.GetComponent<MeshRenderer>().enabled = false;
         cylinderScoot.GetComponent<MeshRenderer>().enabled = false;
         yield return new WaitForSeconds(2);
         transform.position = spawnPoint.transform.position;
 
         cubeScoot.GetComponent<MeshRenderer>().enabled = true;
         cylinderScoot.GetComponent<MeshRenderer>().enabled = true;
 
         Instantiate(deathParticle, transform.position, Quaternion.identity);
     }
 }
 
              Answer by TBruce · Apr 05, 2016 at 01:37 AM
     void OnTriggerEnter(Collider trigger)
     {
         if (trigger.gameObject.CompareTag("Trap"))
         {
             StartCoroutine(PlayerDeath());
         }
     }
 
 
              I got really confused when I first saw your answer and now I'm still confused on Coroutines but that worked... Thank you! :D
NP. You can not call coroutines in C# directly. This can only be don in Javascript. To learn more about coroutines Google "Coroutine unity" and "IEnumerator unity". you may even want to add C# to the search.
Your answer
 
             Follow this Question
Related Questions
Execute coroutine in Update() 8 Answers
Destroy() not removing the object all the time (Coroutine used) 0 Answers
The order of the sound played 0 Answers
Unity Editor can't find IEnumerated 2 Answers