I have a car which is made with Unity Standard Assets, and a cube inside of it as a trigger. Once my player steps in the cube and presses E, a script disables the player movement and camera control, and enabled a camera inside the car, as well as the car controls… in theory that is. Everything works fine, up until the enabling the controls. the code has 2 variables, one for the player, one for the vehicle camera, and one for the vehicle itself.
the car gameobject has 3 components: “Car Controller”, “Car User Control”, and “Car Audio”. By default, these are disabled so that the player doesn’t control the car when not in the car, and the script just enables these components when the player presses E and “enters the car”. Someone said that GetComponent cannot recognize disabled components, but many others, including the youtube tutorial, say it does.
The vehicle camera is a child of the car, and so is the trigger block. The problem is that when the script gets to “GetComponent();”, I get a NullReferenceException. Many forum threads, including another one I made troubleshooting this error have said that the variable isn’t assigned any game object. I made sure that it is, and that the gameobject it’s assigned (the car) has all 3 components. I’m new to unity and coding, and a lot of threads say that getcomponent can’t be used for a gameobject that the script isn’t assigned to. The funny thing is that I used a youtube tutorial for this, and I followed the tutorial step for step, watching it over 20 times the make sure I did nothing wrong. Yet I still get this problem. I’ve tried modifying the code but to no avail.
The Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Vehicles.Car;
public class VehicleEntry : MonoBehaviour
{
public GameObject vehicleCam;
public GameObject thePlayer;
public GameObject liveVehicle;
public bool canEnter = false;
void Update()
{
if (canEnter == true)
{
if (Input.GetKeyDown(KeyCode.E))
{
this.gameObject.GetComponent<BoxCollider>().enabled = false;
vehicleCam.SetActive(true);
thePlayer.SetActive(false);
liveVehicle.GetComponent<CarController>().enabled = true;
liveVehicle.GetComponent<CarUserControl>().enabled = true;
liveVehicle.GetComponent<CarAudio>().enabled = true;
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
canEnter = true;
}
}
void OnTriggerExit(Collider other)
{
canEnter = false;
}
}

These are all the variables under inspector, and they are assigned correctly. Live vehicle is assigned to a GameObject named car, which is the parent of the trigger block that this script belongs to.

the GameObject “Car”, which I just mentioned as the parent has all 3 components.
I believe that this is probably a rookie mistake, but for the life of me I cannot comprehend or find what I did wrong. What’s even worse is that I followed the tutorial correctly! I’m still trying to learn C# and unity, so I really appreciate detailed explanations and help. Thank you in advance to anybody who can assist me with my problem, and I apologize for the poor souls who have to read my giant text wall.



