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 miki98kill · Aug 22, 2015 at 03:56 PM · c#spawn

How do i respawn after the player dies ?

Hi ! I am newbie programmer and i am trying to respawn my player "quadrato" after it falls or hit a specific gameObject ! I started with the first problem: i created an empty sprite with a collider2d and so when the player falls it dies ! And it works ! But i want to respawn it in a specific point of the scene , in the simplest way ; this command "Application.LoadLevel(Application.loadedLevel);"it works but i can't do some checkpoint like in supermario XD And i tried to use "transform.position=new Vector2 (1,1); but it doesn't nothing. Can you help me ? I hope the answer in complete and clear ! Here there is my code !

 using UnityEngine;
 using System.Collections;
 
 public class borderPlayer : MonoBehaviour {
 
     void OnCollisionEnter2D(Collision2D coll){
         if(coll.gameObject.name=="Quadrato")
         {
             Destroy ( coll.gameObject);
             Application.LoadLevel(Application.loadedLevel);
 
         }
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }



 
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

3 Replies

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

Answer by Leoo · Aug 24, 2015 at 07:50 PM

Hello @miki98kill,

First Let's get the concept right, then move to the code.

  1. We want to hide the player 'sprite or renderer" from the scene view so it can't be seen by the user.

  2. We need to know Where we will want to "spawn" back the player object, let's call it a SpawnPoint on the scene.

  3. Move & Activate the player object to that SpawnPoint transform position.

Let's get into the code.

  using UnityEngine;
  using System.Collections;
  
  public class borderPlayer : MonoBehaviour {
      //Create a GameObject on the scene, name it SpawnPoint and put it where do u want to re-spawn your character, then drag it to the "borderPlayer" component, then into the public "SpawnPoint" variable.
      public Transform SpawnPoint;

      void OnCollisionEnter2D(Collision2D coll){
          if(coll.gameObject.name=="Quadrato")
          {
              //Assuming you are using a Sprite Renderer on your character, lets get it and disable it.
              col.GetComponent<SpriteRenderer>().enabled = false;

              //While the character is invisible, teleport it to the SpawnPoint position.
              coll.gameObject.transform.position = SpawnPoint.position;

              //You reload your level, i actually don't quite understand why you are using this.
              Application.LoadLevel(Application.loadedLevel);

               //Then Enable the Character Sprite right after
               col.GetComponent<SpriteRenderer>().enabled = false;
          }
      }
      
      // Update is called once per frame
      void Update () {
          
      }
  }


  • would recommend some improvements like making a function on a GameManager script that handles the spawn of the character / mobs.. after asking for a loadlevel, and keep the "get components" calls minimal by catching them when possible , here i made the " respawn" go instantly, but you could listen for an input on the update and re-enable the character when pressed.*

-Leo

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 miki98kill · Aug 24, 2015 at 08:46 PM 0
Share

Hi @Leoo ! using UnityEngine; using System.Collections;

 public class borderPlayer : $$anonymous$$onoBehaviour {
     public Transform SpawnPoint;
 
     
     // Update is called once per frame
     void Update () {
         }
     void OnCollisionEnter2D(Collision2D coll){
         if(coll.gameObject.name=="Quadrato")
         {
             coll.gameObject.GetComponent<SpriteRenderer>().enabled = false;
             coll.gameObject.transform.position = SpawnPoint.position;
             coll.gameObject.GetComponent<SpriteRenderer>().enabled =true;
         }
     }
 }

You did 3 errors...... but don't worry i solved it :D So really really thak you to help me :) and @Wolfshadow too :)

avatar image
1

Answer by Wolfshadow · Aug 23, 2015 at 02:07 PM

Instead of destroying the player, move it off the scene, effectively hiding it. Then, to respawn, move it to the spot you need.

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 miki98kill · Aug 23, 2015 at 02:20 PM 0
Share

@Wolfshadow I've understand it : but , how can i trduce it in c# ?

avatar image Wolfshadow · Aug 24, 2015 at 04:03 PM 0
Share
  using UnityEngine;
  using System.Collections;
  
  public class borderPlayer : $$anonymous$$onoBehaviour {



      void OnCollisionEnter2D(Collision2D coll){
          if(coll.gameObject.name=="Quadrato")
          {
              transform.Translate(-500, -500, -500);

// move to checkpoint Application.LoadLevel(Application.loadedLevel);

          }
      }
      
      // Update is called once per frame
      void Update () {
          
      }
  }

I couldn't understand trduce, but this is the code, if that is what you need.

avatar image
0

Answer by miki98kill · Aug 23, 2015 at 08:48 AM

Someone can help me ?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Problem with Instantiated clones 1 Answer

Spawn a random object on more than one position 1 Answer

Instantiate spawn problem 1 Answer

[c#] Increase speed of instantiating over time!? 1 Answer

Make a collision check when spawning with Instantiate 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