I have two scripts, one for a BoxCollider2D
Trigger for killing and respawning the player and another for setting a respawn point. When you touch the respawn point, it works perfectly, but when you then touch the kill collider, it returns an error after running through the duration of the respawn pause stating that respawnPoint
is null.
For context, when I say modular respawn system, I mean a system where respawnPoint
is the position of the latest one you've hit rather than one singular point.
I tried everything I could think of but I just can't figure it out. Anyone know what's wrong? (And maybe have some suggestions for how I could make the player disappear during the respawn window and reappear when respawned)
Here are the scripts:
Respawn Point:
public GameObject player;
public Vector2 respawnPoint;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
respawnPoint = player.transform.position;
Debug.Log("Respawn Point Set");
}
}
KillingZone:
public GameObject player;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
StartCoroutine(Respawn());
}
IEnumerator Respawn()
{
Debug.Log("Entered Killing");
//Destroy Player
yield return new WaitForSeconds(1.5f);
Debug.Log("Respawn");
player.transform.position = gameObject.GetComponent<RespawnPoint>().respawnPoint;
}
}