Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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 AlexTheHollow · Jul 23, 2015 at 05:44 PM · playermonodeveloprespawnscipting

Player Respawn Script

I'm trying to get my player to respawn upon colliding with the enemy but every time they die, they just appear somewhere far off in the level (I assume).

Here's my script:

 using UnityEngine;
 using System.Collections;
 
 public class Respawn : MonoBehaviour {
 
     public GameObject player;
     public Transform SpawnPoint;
 
     void OnTriggerEnter (Collider col)
     {
         if     (col.tag == "Player")
         {
             player.transform.position = SpawnPoint.transform.position;
          }
     }
 }


I've looked online and this has been the closest one to work for me other than this single problem. Help?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Perimetric · Jul 23, 2015 at 06:31 PM

Check your transform position and try this :

 using UnityEngine;
 using System.Collections;
 
 public class Respawn : MonoBehaviour {
 
     public GameObject spawnPoint;
   
     void OnTriggerEnter (Collider col)
     {
         if(col.transform.tag == "Enemy")
         {
              this.transform.position = spawnPoint.transform.position;
         }
     }
 }

Attach this script to your player and add a tag "Enemy" to your enemy gameobject.

Comment
Add comment · Show 11 · 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 AlexTheHollow · Jul 23, 2015 at 08:11 PM 0
Share

No good... Same thing is happening.

I have another script called EnemyAI that will track down the player and kind of follow/collide with them.

Script:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy_AI : $$anonymous$$onoBehaviour {
 
     Transform tr_Player;
     float f_RotSpeed=3.0f,f_$$anonymous$$oveSpeed = 5.0f;
     
     // Use this for initialization
     void Start () {
         
     tr_Player = GameObject.FindGameObjectWithTag ("Player").transform; }
     
     // Update is called once per frame
     void Update () {
         /* Look at Player*/
         transform.rotation = Quaternion.Slerp (transform.rotation , Quaternion.LookRotation (tr_Player.position - transform.position) , f_RotSpeed * Time.deltaTime);
         
         /* $$anonymous$$ove at Player*/
         transform.position += transform.forward * f_$$anonymous$$oveSpeed * Time.deltaTime;
     }
 }


It comes up with the error:

$$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

Could they be linked?

avatar image Perimetric · Jul 23, 2015 at 09:09 PM 0
Share

Do you have any script using Destroy(); ?

avatar image AlexTheHollow · Jul 23, 2015 at 11:45 PM 1
Share

Yes. This is used to kill the player when the Enemy collides them.

  #pragma strict
  
  // Delete whatever touches this object
  // $$anonymous$$eeping in $$anonymous$$d a tag set in the Inspector values
  // Improvements: Add Array of tags for multi-functionality
  
  var tagTo$$anonymous$$ill : String = "Player";                        // Other object name, will be deleted on touch
  
  function OnTriggerEnter ( other : Collider ) {            // TriggerEnter function requires a box collider marked as Trigger on this or on other object
  
      if ( other.gameObject.CompareTag( tagTo$$anonymous$$ill )){        // Check killTag, if equal delete other object
          GameObject.Destroy( other.gameObject );
      }
      
  }
  
  function OnTriggerExit ( other : Collider ) {            // Repeat TriggerEnter function to add another layer of collision detection to $$anonymous$$imize missing collisions due to performance
  
      if ( other.gameObject.CompareTag( tagTo$$anonymous$$ill )){
          GameObject.Destroy( other.gameObject );        
      }
      
  }
avatar image JustinTheDev · Jul 24, 2015 at 03:57 AM 0
Share

What is your Respawn script attached to?

avatar image AlexTheHollow · Jul 24, 2015 at 04:34 AM 0
Share

The player.

Show more comments
avatar image
1

Answer by Hexer · Jul 26, 2015 at 08:54 PM

Your logic is a bit strange. First, this script should be on another object instead of the player. Second, Why are you also using transform on the Spawnpoint. I imagine because of that the spawnpoint keeps transforming. And last, you don't need public GameObject player if you reference it as col in your script.

I rewrote it a bit for you. (Your first script was close, but you forgot somethings)

 using UnityEngine;
 using System.Collections;
 
 public class Respawn : MonoBehaviour {
 
     public Vector3 SpawnPoint;
     
     void OnTriggerEnter (Collider col)
     {
         if     (col.tag == "Player")
         {
             col.transform.position = SpawnPoint;
         }
     }
 }

Instead of public Transform SpawnPoint; I used Vector3. You can change its (x,y,z) in the inspector, what you fill for those values will be the spawnposition. Then I changed player.transform.position = SpawnPoint.transform.position;to col.transform.position = SpawnPoint;this lets me transform the col to the spawnpoint position that we set earlier as a Vector3(x,y,z)

And at last I removed public GameObject Player; because this was in my eyes not needed.

I hope my modification on your code and my explanation have helped you a bit.

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 AlexTheHollow · Jul 27, 2015 at 07:41 AM 0
Share

What should be removed (script(s) wise) and what scripts should be relocated?

Where does this one go?

avatar image
0

Answer by minetera2 · Aug 04, 2015 at 12:52 PM

try to tell unity what is the Spawn Point by going some thing like :

 public GameObject SpawnPoint = gameObject findWithTag(//here put your tag);

and put this on the player GameObject tahats what l think you shud do :)

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
avatar image
0

Answer by jamesk5 · Aug 04, 2015 at 02:43 PM

You are referencing Transform twice:

 public Transform SpawnPoint;
 player.transform.position = SpawnPoint.transform.position;

Try this:

 public Transform SpawnPoint;
 player.transform.position = SpawnPoint.position;

Or this:

 public GameObject SpawnPoint;
 player.transform.position = SpawnPoint.transform.position;


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
avatar image
0

Answer by jonahvendraws · Apr 21, 2021 at 07:21 AM

I am having problems, mostly because none of these have worked, I am making somewhat of a floor is lava type game, and when I set the player as a player tag and set the spawnpoint, it just sinks through the floor

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

8 People are following this question.

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

Related Questions

How do I respawn the Player after it dies? 3 Answers

Player Respawn with Joystick 2 Answers

Upgrades not applying after first death 2 Answers

Problems with a player spawning and handling script 1 Answer

Player can't move upon respawning in a new position. 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