Exported 'Loose Collider' from Block Breaker, not working?

So, I recently started a platformer project, and just today discovered you guys were adding this new 2D platformer section to the course!
My issue is, im trying to use the LevelManager from previous sections in this game, my menu is working just fine, but when I try to load the lose screen with a lose collider, by making the player touch the collider, unity gives me this error: NullReferenceException: Object reference not set to an instance of an object LoseCollider.OnCollisionEnter2D (UnityEngine.Collision2D collision) (at Assets/Scripts/LoseCollider.cs:14)

Game Scene:

LevelManager script:

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

public class LevelManager : MonoBehaviour {

    public void LoadLevel(string name) {
        Debug.Log("Level load requested for: " + name);
        Application.LoadLevel(name);

    }

    public void QuitRequest() {
        Debug.Log("Quit Requested");

    }
    public void LoadNextLevel() {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}

Loose Collider script:

using System.Collections.Generic;
using UnityEngine;

public class LoseCollider : MonoBehaviour {

private LevelManager levelManager;

    void Start() {
        levelManager = GameObject.FindObjectOfType<LevelManager>();
    }

    void OnCollisionEnter2D(Collision2D collision) {
        levelManager.LoadLevel("Loose Screen");
    }
    void OnTriggerEnter2D(Collider2D trigger) {
        print ("trigger");
        
    }
}

I dont know what is wrong, thanks in advance :slight_smile:

Hi madru,

it looks like you don’t have a levelManager game object in your scene, so the function FindObjectOfType() can’t return anything.

1 Like

Oh god, im laughing at myself now, so obvious yet after about 30 minutes of trying to find the problem, I couldnt see this. Thank you very much. Sebastian :slight_smile:

1 Like

No problem :wink:

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

Privacy & Terms