For some reason Object reference not set to instance of an object

I got an error in the console saying object reference not set to instance of an object. The error in the console is not very clear on telling me which thing is not set to an instance of object. It just tells me the line that causes the error. I can’t figure out what’s wrong with my code.

Here is the error:

NullReferenceException: Object reference not set to an instance of an object
LockWallScript.OnCollisionEnter2D (UnityEngine.Collision2D col) (at Assets/EditorAssets/Scripts/LockWallScript.cs:7)

and here is my code simplified:

LockWallScript:

public class LockWallScript : MonoBehaviour {

	public bool locked = true;

	void OnCollisionEnter2D (Collision2D col) {
		if (col.gameObject == GameObject.Find("Player")) {
				Game.game.findScene (SceneManager.GetActiveScene ().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
		}
	}

	void Start () {
		locked = true;
	}
}

Game class:

[System.Serializable]
public class Game {

	public static Game game;

	public List<SceneLayout> scenes;

	public Game () {
		scenes = new List<SceneLayout>();
	}

	public SceneLayout findScene (string nm) {
		bool sceneAvailable = false;
		SceneLayout scene = null;

		foreach (SceneLayout i in scenes) {
			if (i.name == nm) {
				sceneAvailable = true;
				scene = i;
			}
		}

		if (sceneAvailable) {
			return scene;
		} else if (!sceneAvailable) {
			return null;
		}

		return null;
	}
}

SceneLayout class:

[System.Serializable]
public class SceneLayout {
	public string name;

	public List<int> unlockedLockWalls;

	public SceneLayout (string nm) {
		name = nm;
	}
}

GameplayControlScript:

public class GameplayControlScript : MonoBehaviour {

	public static GameplayControlScript gameplay;

	void Awake () {
		gameplay = this;
		Game.game = new Game ();
		Game.game.scenes.Add(new SceneLayout(SceneManager.GetActiveScene ().name))
	}

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

Can anyone tell me what is causing the error?

I think your LockWallScript dont have any reference to your game class.

 public class LockWallScript : MonoBehaviour {
 
     public bool locked = true;
     public Game game;
 
     void OnCollisionEnter2D (Collision2D col) {
         if (col.gameObject == GameObject.Find("Player")) {
            if(game != null)
            {
                   if (game.worldKeys > 0 && locked) {
                   game.findScene (SceneManager.GetActiveScene 
                    ().name).unlockedLockWalls.Add (transform.GetSiblingIndex ());
                    }
           }
         }
     }
 
     void Start () {
         locked = true;
     }
 }

On Inspector, drag Object with script Game to the new field.

I just found out what was causing the error! When I create a new SceneLayout() I just have to set the unlockedLockWalls to a new List()

Here is the SceneLayout class:

[System.Serializable]
 public class SceneLayout {
     public string name;
 
     public List<int> unlockedLockWalls;
 
     public SceneLayout (string nm) {
         name = nm;
         unlockedLockWalls = new List<int>();
     }
 }