Can't get my Level Exit game object to detect collision with Player game object

I’m not getting any errors or Debug.Log messages in the console when Player makes contact with Level Exit, even though I put Debug.Log lines in both OnCollisionEnter2D() and LoadNextLevel(). Am I overlooking something?

help1

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

public class LevelExit : MonoBehaviour {

    [SerializeField] float LevelLoadDelay = 2f;

    void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collision detected");
        StartCoroutine(LoadNextLevel());
    }

    IEnumerator LoadNextLevel()
    {
        yield return new WaitForSecondsRealtime(LevelLoadDelay);
        Debug.Log("LoadNextScene() called");
        var currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex + 1);
    }
}

Just tried allowing the Enemy layer to collide with the Interactables layer. The enemy flips when leaving Level Exit’s collider, but still doesn’t trigger anything in LevelExit.cs, indicating that it’s likely an issue with the Level Exit game object.

Found my mistake. I accidentally used OnCollisionEnter2D instead of OnTriggerEnter2D. So this:

void OnCollisionEnter2D(Collision2D collision)

Should have been this:

void OnTriggerEnter2D(Collider2D collision)

Apologies if anyone started trying to help with this already. Hopefully my documentation of the mistake here can help someone else in the future.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms