• 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
Question by infinite_progress · Feb 07, 2014 at 05:13 PM · playerrespawn

How do I respawn the Player after it dies?

I'm trying to make a 2D side-scrolling space shooter and I made a Player which has 3 lives and is supposed to respawn when it dies until lives = 0.

Here's my script written in C#, but it's not working correctly... I'm new to this, can anyone help?

 using UnityEngine;
 using System.Collections;
 
 public class Death : MonoBehaviour {
 
     public int lives = 3;
     public GameObject Player = GameObject.FindGameObjectWithTag("Player");
 
     void  OnTriggerEnter2D ( Collider2D col  ){
         lives--;
         if (col.gameObject.tag == "Enemy") {
             Destroy(gameObject);
             if(lives > 0){
                 GameObject PlayerClone = (GameObject)Instantiate(Player, new Vector2(0,0), Quaternion.identity);
             }
             //else RESTART/MENU (i'll write this later)
         }
     }
 }
Comment
Andrew_Kenady
jaywyxyr

People who like this

2 Show 1
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 Jeremy2300 · Feb 07, 2014 at 06:09 PM 0
Share

Do you need to Destroy the gameObject?

My respawns tend to just take the player on death and transform that object to a new (respawn) position.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by kk93 · Feb 07, 2014 at 06:49 PM

You really don't need to destroy the gameObject when your character is "killed" just use renderer.enabled = false, then renderer.enabled = true to respawn it.

With that said, the way you have it set up, you won't even see it disappear. Try this:

 void  OnTriggerEnter2D ( Collider2D col  ){
     lives--;
     if (col.gameObject.tag == "Enemy") {
         if (lives > 0) {
             StartCoroutine (Dead ());
         }
     }

 IEnumerator Dead() {
     Debug.Log ("dead");
     renderer.enabled = false;
     yield return new WaitForSeconds(5);
     Debug.Log ("respawn");
     renderer.enabled = true;
 }
Comment
infinite_progress
unity_3DmGkY57st7DXQ
joliveriwp

People who like this

3 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 infinite_progress · Feb 08, 2014 at 10:26 AM 0
Share

Thank you! :D However, in your script you were missing a "}" before the IEnumerator line. I also added the "gameObject.transform.position" line as the other answer suggested and it works great :)

avatar image

Answer by Andrew_Kenady · Feb 07, 2014 at 06:34 PM

I assume that this script is added to the player, yes? If so, your issue lies in this part:

 if (col.gameObject.tag == "Enemy") {
          Destroy(gameObject);

In this line, you've destroyed the gameobject that the Death script is attached to. Most likely, what you'd want to do instead is to move [`gameobject.transform.position`] back to the starting position.

If you're looking for just a simple way to restart the level altogether, you can call:

 Application.LoadLevel(Application.loadedLevel);

Hope this helps!

Comment
infinite_progress

People who like this

1 Show 0 · 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

Answer by Zassa · Dec 08, 2015 at 05:32 PM

I put in this script for a character respawn:

using UnityEngine; using System.Collections;

public class PlayerDie : MonoBehaviour {

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
     
 }
 void OnCollisionEnter2D(Collision2D collision){
     if (collision.gameObject.name "End") {
         Destroy(gameObject);        
     }
 }
 
 //player respawn
 
 if (gameObject Destroy){
     current.transformation.position new vector3(10, 0, 300);
 }

But now it doesn't seem to work. It tells me there is a parsing error on line 24, and that "End" is an unknown entity. Can anyone help me?

Comment

People who like this

0 Show 0 · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Player Respawn Script 5 Answers

Player will not Die or Respawn 1 Answer

Respawn Time 1 Answer

Random Player Respawn Points 3 Answers

How do I make my player re-spawn in its original place on collision of enemy? 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