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;
}
}