Respawn code not working in Unity, Can anyone help me?

Viewed 49

My code doesn't work. The player is supposed to respawn when a player with the tag "Player" interacts with an object with this code. Can someone help me, thanks in advance! Code ☟

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class KillPlayerLava : MonoBehaviour
{
    public int Respawn;
    public float Seconds;
    public MonoBehaviour script;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public IEnumerator OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            yield return new WaitForSeconds(Seconds);
            SceneManager.LoadScene(Respawn);
            script.enabled = false;
        }
    }
}
1 Answers

Here are something which might help you find the answer, the code you posted looks fine but there can be other things causing the issue.

Does your gameobject have a 2D collider?

Does one of the objects that it's colliding with have a Rigidbody2D?

Note: Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached. Trigger events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. Source docs : MonoBehaviour.OnTriggerEnter2D(Collider2D)

Does your play have the Player tag set?

Related