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.