- Home /
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?
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.
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?
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 );
}
}
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.
What should be removed (script(s) wise) and what scripts should be relocated?
Where does this one go?
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 :)
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;
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
Your answer
![](https://koobas.hobune.stream/wayback/20220611232634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player Respawn Script - Not Working 1 Answer
Player Respawn After Death 1 Answer
how to spawn and destroy 2 Answers
Respawning in UnityIphone? 2 Answers
Player taking damage on collision. Can't get the script to work!? 1 Answer