Why?? MissingComponentException:

MissingComponentException:There is no ‘GameObject’ attached to the “Car” game object, but a script is trying to access it. You probably need to add a GameObject to the game object “Car”… Or your script needs to check if the component is attached before using it. Car.Update () (at Assets/Scripts/Car.cs:40)

Hello,

I have just started programming in Unity, and everything has been working fine, and for no reason, when I click, play in unity controller, the attached game object disconnects, and the text labels I have attached also change why is this?

Thank You,
Marcell

Here is my code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;


public class Car : MonoBehaviour {

	public GameObject Car1;
	public GameObject FirstPersonController;
	public Camera CarCam;
	public Camera FirstPersonControllerCam;
	public Text CarGetIn;
	public Button CarGetInBut;
	public Text CarGetOut;
	public Button CarGetOutBut;
	int inCar=0;
	float dist;

	// Use this for initialization
	void Start () {
		Car1 = Car1.GetComponent<GameObject> ();
		FirstPersonController = FirstPersonController.GetComponent<GameObject> ();
		CarCam = CarCam.GetComponent<Camera> ();
		FirstPersonControllerCam = FirstPersonControllerCam.GetComponent<Camera> ();
		CarGetIn = CarGetIn.GetComponent<Text> ();
		CarGetInBut = CarGetInBut.GetComponent<Button> ();
		CarGetOut = CarGetIn.GetComponent<Text> ();
		CarGetOutBut = CarGetInBut.GetComponent<Button> ();
		CarGetIn.enabled = false;
		CarGetInBut.enabled = false;
		CarGetOut.enabled = false;
		CarGetOutBut.enabled = false;


	}
	void Update (){

	if (inCar == 0) {
			//dist = Vector3.Distance(Car1.transform.position, FirstPersonController.transform.position);
			if (dist < 5.00) {
				CarGetIn.enabled = true;
				CarGetInBut.enabled = true;
				//print ("Test");
			} else {
				CarGetIn.enabled = false;
				CarGetInBut.enabled = false;
				//	print ("Test");
			}
			//print (dist);


		} else {
			CarGetOut.enabled=true;
			CarGetOutBut.enabled=true;
		}
	}
	public void EnterCar(){

			inCar = 1;
		Car1.SetActive (true);
		CarCam.enabled = true;
		FirstPersonController.SetActive (false);
		FirstPersonControllerCam.enabled = false;
		CarGetIn.enabled = false;
		CarGetInBut.enabled = false;


	}
	public void LeaveCar() {
		inCar = 0;
		//Car1.SetActive (false);
		CarCam.enabled = false;
		FirstPersonController.SetActive (true);
		FirstPersonControllerCam.enabled = true;
		CarGetOut.enabled = false;
		CarGetOutBut.enabled = false;

	}
}

Those lines won’t compile at all and don’t make any sense:

Car1 = Car1.GetComponent<GameObject> ();
FirstPersonController = FirstPersonController.GetComponent<GameObject> ();

A GameObject is not a component and can’t be passed as generic parameter to GetComponent as only types which are derived from Component are allowed.

Remove those two lines.

Further more those lines won’t throw a compile error but make no sense either:

     CarCam = CarCam.GetComponent<Camera> ();
     FirstPersonControllerCam = FirstPersonControllerCam.GetComponent<Camera> ();
     CarGetIn = CarGetIn.GetComponent<Text> ();
     CarGetInBut = CarGetInBut.GetComponent<Button> ();

As well as those two which even seem to have a copy and paste error as you use “CarGetIn” and assign it to CarGetOut:

     CarGetOut = CarGetIn.GetComponent<Text> ();
     CarGetOutBut = CarGetInBut.GetComponent<Button> ();

All those GetComponent lines are total nonsense as you already assigned those references directly in the inspector.

Hi, Thanks for your help, I figured it out by using transforms instead of gameobjects.