Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ramsey111 · Apr 05, 2016 at 01:32 AM · c#coroutine

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);
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Apr 05, 2016 at 01:37 AM

@Ramsey111

     void OnTriggerEnter(Collider trigger)
     {
         if (trigger.gameObject.CompareTag("Trap"))
         {
             StartCoroutine(PlayerDeath());
         }
     }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ramsey111 · Apr 08, 2016 at 01:07 PM 0
Share

I got really confused when I first saw your answer and now I'm still confused on Coroutines but that worked... Thank you! :D

avatar image TBruce Ramsey111 · Apr 08, 2016 at 04:37 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Coroutine is called twice less time than update 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges