I am completely new to using unity and this is my first project (a 2d platformer). I keep following tutorials on youtube but I still don’t know what’s wrong with my script. I am trying to add a respawn time which waits for an amount of time before respawning after you die but all that happens is that the player dies. I can make it that the player respawns instantly but I don’t like it that way. this the script that im using:
Player death script:
public class PlayerDeath : MonoBehaviour
{
public GameObject DeathEffect;
public Transform Player;
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Danger"))
{
StartCoroutine("Rspawn");
GameObject Effect = Instantiate(DeathEffect, Player.position, Quaternion.identity);
Destroy(gameObject);
Destroy(Effect, 2f);
}
}
IEnumerator Rspawn()
{
yield return new WaitForSeconds(2);
Debug.Log("Respawning");
LevelManager.instance.Respawn();
Debug.Log("Respawned");
StopCoroutine("Rspawn");
}
}
and if you need it here is my level manager script:
public class LevelManager : MonoBehaviour
{
public static LevelManager instance;
public Transform RespawnPoint;
public GameObject PlayerPrefab;
public CinemachineVirtualCameraBase cam;
private void Awake()
{
instance = this;
}
public void Respawn()
{
GameObject Player = Instantiate(PlayerPrefab, RespawnPoint.position, Quaternion.identity);
cam.Follow = Player.transform;
}
}
There aren’t any errors showing up in console so I don’t know what the problem is.