using UnityEngine;
using System.Collections;
public class Respawn : MonoBehaviour {
// Use this for initialization
public GameObject other;
void OnTriggerEnter2D (Collider2D other) {
if (other.gameObject.tag == "Car1") {
transform.position = new Vector3 (0f,0f,0f); //<---wanted to respawn to this position
}
}
}
The way I would do it is make the player into a prefab. Make an empty gameobject where you want to player to respawn. Then add a script like this to the object that triggers the player death:
public GameObject carl;
public Transform spawnPoint;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Carl")
{
Destroy(other.gameObject);
StartCoroutine("Respawn", 5f);
}
}
IEnumerator Respawn(float spawnDelay)
{
yield return new WaitForSeconds(spawnDelay);
Instantiate (carl, spawnPoint.position, spawnPoint.rotation);
}
You see, when the player hits the trigger I destroy the player and respawn him from a prefab, using the IEnumerator. If you need to player to keep data then instead of destroying him you can just disable his gameobject in the trigger event, and then re-enable it in the IEnumerator.