NullReferenceException: Object reference not set to an instance of an object

I’ve got a simple script called DoorController written in C#.

using UnityEngine;
using System.Collections;
public class DoorController : MonoBehaviour {
	public GameObject door;
	public GameObject button;
	private bool doorOn;
	private bool buttonOn;
	// Use this for initialization
	void Start () {
		door = new GameObject();
		button = new GameObject();
		doorOn = door.GetComponent<Door>().buttonOn;
		buttonOn = button.GetComponent<Button>().buttonActive;
	}
	
	// Update is called once per frame
	void Update () {
		
		if(buttonOn == true){
			doorOn = true;
		}
		else if(buttonOn == false){
			doorOn = false;
		}
	}
}*

The script reads the state of a button in the scene and changes the state of a door in the same scene. However, when I run the scene, I get the following error:

NullReferenceException: Object reference not set to an instance of an object
DoorController.Update () (at Assets/Scripts/DoorController.cs:18)

I’ve referenced both the Door and the Button object in the inspector. I can’t figure out why this won’t work.

The issue is line 10 and 11. You are overwriting the values you set in the inspector for ‘door’ and ‘buttonOn’ with new game objects that don’t have Door and Button components. I’m not sure what you are attempting with lines 10 and 11. Take them out, and your error should go away.