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?

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.

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.

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 :slight_smile:

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;

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