Problem with making a player respawn in unity 2D (C#)

I’m writing a code for a platformer and I can’t figure out how to code exactly the respawn moment. I have determined that my respawn point works correctly, and that the character making contact with the death points (enemies and spikes) does trigger the if of my if, then statement. From this I can safely determine that my problem is my then statement. I believe I’m using an outdated phrase (it worked in Unity 4 at some point, I’m fairly sure). Anyway here’s the “then” part of my if, then statement, meant to put my character back to the spawn point:
Player.transform.position = spawnPoint.transform.position;

I know that everything else works because I also have this in my if, then statement:
Debug.LogWarning (“Player Respawn”);
and I get the warnings when the player hits the spikes.

Hi !

I think you can first declare

public GameObject PlayerPrefab;

And assign it in the Editor to the Player Prefab then use this in the “Then” statement.

Destroy (Player);
Instantiate (PlayerPrefab, spawnPoint.transform.position, Quaternion.identity);

This way, you’ll be destroying the Player and instantiating a new one at the spawnPoint.

It seems to me that you don’t properly understand the correct use of Unity and scripts, which is totally normal as you just started. I recommend you to take the time to follow some detailed tutorials such as this one. The guy writing the code gives great advice on how to organize the scripts and what script should handle what etc.

At one point they do cover the respawn of the player.

Here ya go. Here’s the whole thing.

using UnityEngine;
using System.Collections;

public class KillPlayer : MonoBehaviour {

	public GameObject spawnPoint;
	private newabu Player;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
		Player = FindObjectOfType<newabu> ();
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.name == "Player") {
			Debug.LogWarning ("Player Respawn");
			Player.transform.position = spawnPoint.transform.position;
		}
	}
}

newabu is the script used to make the player move. Player is the only object that has newabu, and I get the “respawn player” warning when the player makes contact with spikes (they have this script attached to them). The respawn point is set at the beginning of the level.

Avoid instantiating gameobjects always. Its too costly for performance.
Simple trick.
Create a vector3 that stores players position whilst he is alive.
Instead of destroying gameObject Get the renderer and disable it.
transform the players position to the stored vector3 then enable the renderer. ’
This is best done with coroutine.
This way u can set various waiting times to do all things.
Also remember to Disable and reenable all physics components .
Finally in all your script remember if u have any reference to those disabled elements make check for it before doing something.