Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by AppleJuicy · Jan 26, 2015 at 04:37 AM · delayrespawn

how to respawn with a simple delay?

using UnityEngine; using System.Collections;

public class Respawn : MonoBehaviour {

 // Use this for initialization

 
 public GameObject other;


 void OnTriggerEnter2D (Collider2D other) {
 if (other.gameObject.tag == "Car1") {  
         
 transform.position = new Vector3 (0f,0f,0f); //<---wanted to respawn to this position
         
     }

}

}

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

Answer by nightowl79a · Jan 26, 2015 at 12:09 PM

The way I would do it is make the player into a prefab. Make an empty gameobject where you want to player to respawn. Then add a script like this to the object that triggers the player death:

      public GameObject carl;
      public Transform spawnPoint;
     
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Carl")
         {
             Destroy(other.gameObject);
             StartCoroutine("Respawn", 5f);
         }
     }
     
     IEnumerator Respawn(float spawnDelay)
     {
         yield return new WaitForSeconds(spawnDelay);
         Instantiate (carl, spawnPoint.position, spawnPoint.rotation);
     }

You see, when the player hits the trigger I destroy the player and respawn him from a prefab, using the IEnumerator. If you need to player to keep data then instead of destroying him you can just disable his gameobject in the trigger event, and then re-enable it in the IEnumerator.

Comment
Add comment · Show 5 · 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 Rock_Rafy · Jul 17, 2020 at 09:25 AM 0
Share

"if you need the player to keep data then instead of destroying him you can just disable his game object in the trigger event, and then re-enable it in the IEnumerator". Can you please explain with same example. Thank you.

avatar image nightowl79a Rock_Rafy · May 16, 2021 at 09:12 PM 0
Share

https://docs.unity3d.com/ScriptReference/GameObject.SetActive.html other.gameobject.setactive(false)

setting active to false keeps the game object in the scene, but makes it almost gone, no render, or colliders, or anything like that, and variable's set in scripts like maybe HP for example stay the same, unlike gameobject.destroy, witch removes it from the scene completely.

avatar image malthew · May 16, 2021 at 12:14 PM 0
Share

What does the value "5f" in the parameters of the StartCoroutine function do? If it's the amount of time it will delay, then how does it connect to the parameter "spawnDelay" in the Respawn function?

avatar image Hellium malthew · May 16, 2021 at 12:16 PM 0
Share

Take a look at the documentation

https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

The second argument of StartCoroutine using a string will be passed to the actual coroutine method.

Today, it's highly advised to use the following way to start a coroutine.

  StartCoroutine(Respawn(5f));
avatar image nightowl79a malthew · May 16, 2021 at 09:07 PM 0
Share

f is for float. all floats require "f" in front of them. I guess I should have used a variable instead, but it was just a quick write.

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

6 People are following this question.

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

Related Questions

How to delay a respawn? 1 Answer

Dealing with nulls on Respawn 1 Answer

Fade on death! 1 Answer

2D project - player respawn delay problem 0 Answers

How to make my espawn delay trigger to work more than once. 2 Answers


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