Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Gusic · Feb 18, 2014 at 04:16 PM · c#2dbeginnerrespawn

Help Me with simple respawn script please (c#)

Hi guys, I have made a Player Die script for my 2d game so when the player hits the object(Spikes) he dies but i am trying to make it so when the player dies he respawns a couple of seconds later in coordinates (0.04833326, 3.980667, 0), here is my script i cant seem to get it to work.

Thank You

 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 == "Spikes") {
             Destroy(gameObject);        
         }
     }
 
 //player respawn
 
 if (gameObject == Destroy){
   current.transformatio.position = new vector3(0.04833326, 3.980667, 0);
    }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by robertbu · Feb 18, 2014 at 04:35 PM

When you call 'Destory()' on line 18, this script and the game object it was attached to ceases to exist. As an alternate to destroying the game object, consider hiding it, moving it and then showing it again.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDie : MonoBehaviour {
     
     void OnCollisionEnter2D(Collision2D collision){
         if (collision.gameObject.name == "Spikes") {
             StartCoroutine(DieAndRespawn()); 
         }
     }
 
     IEnumerator DieAndRespawn() {
         renderer.enabled = false;
         yield return new WaitForSeconds(2.0f);
         transform.position = new Vector3(0.04833326f, 3.980667f, 0.0f);
         transform.rotation = Quaternion.identity;
         renderer.enabled = true;
     }
 }

In order to "respawn" this way there may be additional things you have to do. You may have to turn colliders off and back on, or reset the Ribidbody.velocity to 0.0.

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 RudyTheDev · Feb 18, 2014 at 04:51 PM 0
Share

Also (unless your player actually disappears on death), it may be worth/nice having some sort of death state. Then respawning is simply "resetting" the player back to life.

avatar image
0

Answer by moha_kun · Aug 04, 2016 at 03:10 PM

try this

 using UnityEngine;
 using System.Collections;
 
 public class playermovements : MonoBehaviour {
 
     public float speed;
     private Rigidbody rb;
 
     private Vector3 spown;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody> ();
         spown = transform.position;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         speed = 25;
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         Vector3 movement = new Vector3 (moveHorizontal,0, moveVertical);
         rb.AddForce (movement * speed);
 
 
     }
     void OnCollisionEnter (Collision other)
     {
         if(other.transform.tag == "enimy")
         {
             transform.position = spown;
         }
     }
 
 }





Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Respawn Die Script (C#) 1 Answer

I'm using an Object Pooler for the projectiles in my game. How do I reset their movement speed when they are made active again? 1 Answer

Button not working but other buttons are working. HELP 0 Answers

2D boomerang effect 1 Answer

How to detect an object inside another object? 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