• 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 1 · 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.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

How to make an enemy respawn? 0 Answers

Respawning a player after a certain amount of time 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges