iowaqas
iowaqas

Reputation: 161

OnCollisionEnter2D doesn't restart my scene

I would like my player to collide with an invisible collider and restart the scene right away but it seems like my script isn't working for some reason.

  1. Added the tag 'Fall' on the invisible collider.
  2. Added the script on the Invisible Collider Game Object.
  3. Invisible collider object has Box Collider 2D Attached and is set ti: is Trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class sceneRestart : MonoBehaviour
{  
    private void OnCollisionEnter2D(Collision2D platform)
    {
        if (platform.gameObject.tag == "Fall")
        {
            SceneManager.LoadScene("Main");
            Debug.Log("Scene Restarted");
        }
    }
}

Upvotes: 1

Views: 31

Answers (1)

iowaqas
iowaqas

Reputation: 161

The script is attached to the invisible collider, so it needs to check if the colliding object is the player:

public class sceneRestart : MonoBehaviour
{

    private void OnTriggerEnter2D(Collider2D Col)
    {
        if (Col.gameObject.tag == "Player")
        {
            SceneManager.LoadScene("Main");
            Debug.Log("Scene Restarted");
        }
    }

Upvotes: 1

Related Questions