I’m trying to make an item respawn when the player respawns, but it keeps coming up with NullReferenceException and preventing my player from respawning.
The first script :
private HealthItem potion;
public IEnumerator RespawnPlayer()
{
yield return new WaitForSeconds(5f);
StartCoroutine(potion.Respawn());
this.gameObject.GetComponent<BoxCollider2D>().enabled = true;
animator.SetTrigger("ResetAnim");
rb.gravityScale = 0;
rb.linearVelocityY = 0;
yield return new WaitForSeconds(0.3f);
currentHealth = maxHealth;
HealthUI.UpdateHealth(currentHealth);
rb.gravityScale = 1;
rb.position = respawn.position;
camcontrols.enabled = true;
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
yield return new WaitForSeconds(0.001f);
camcontrols.Damping = new Vector3(1, 1, 0);
}
The script I’m trying to call:
public class HealthItem : MonoBehaviour
{
public IEnumerator Respawn()
{
yield return new WaitForSeconds(5f);
Debug.Log("If you're seeing this, the respawn logic works!");
}
}
If anyone has a solution for this please let me know.