Need help fixing error (NullReferenceException: Object reference not set to an instance of an object)
So here's my code:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Vehicles.Car;
using UnityStandardAssets.Characters.FirstPerson;
public class ControllerManager : MonoBehaviour {
private CarUserController CarController;
private RigidbodyFirstPersonController Player;
private CarAudio CarSound;
public Camera PlayerCamera;
public Camera CarCamera;
// Use this for initialization
void Start ()
{
CarController.enabled = false;
Player.enabled = true;
CarSound.enabled = false;
CarCamera.enabled = false;
PlayerCamera.enabled = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
SwitchCamera();
}
}
void SwitchCamera()
{
CarController.enabled = !CarController.enabled;
CarCamera.enabled = !CarCamera.enabled;
CarSound.enabled = !CarSound;
Player.enabled = !Player.enabled;
PlayerCamera.enabled = !PlayerCamera.enabled;
}
}
I get the error on line 15: CarController.enabled = false; I made this script for switching the player and car controllers. It's weird because the car controller is getting the error, but the player controller isn't (Player is the player controller btw). Anyway, anyone know where I went wrong?
EDIT: I just realized that the same thing is happening with all of Unity's car scrips.... almost same exact thing. Here's the one for cross-platforming controlls:
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Vehicles.Car
{
[RequireComponent(typeof (CarUserController))]
public class CarUserControl : MonoBehaviour
{
private CarUserController m_Car; // the car controller we want to use
private void Awake()
{
// get the car controller
m_Car = GetComponent<CarUserController>();
}
private void FixedUpdate()
{
// pass the input to the car!
float h = CrossPlatformInputManager.GetAxis("Horizontal");
float v = CrossPlatformInputManager.GetAxis("Vertical");
#if !MOBILE_INPUT
float handbrake = CrossPlatformInputManager.GetAxis("Jump");
m_Car.Move(h, v, v, handbrake);
#else
m_Car.Move(h, v, v, 0f);
#endif
}
}
}
The error is on line 27: m_Car.Move(h, v, v, handbrake);
you're not initializing CarController
in Controller$$anonymous$$anager
in the second script, does the game object that you've attached it to also have the CarUserController
component?
both of those will cause null reference exceptions...
@gjf All right. How do I initialize CarController in Controller$$anonymous$$anager?
As for Unity's scripts, I don't know much. The Controller$$anonymous$$anager script is the first 100% original script I've made, and Unity's stuff is still pretty complicated to me. But the car came from the official vehicle package as a prefeb - everything was already put together. The car does have the CarUserController component though. I think I'll try re-importing the package and try again, but I doubt that will work. If you want to look into it, I'm sure everyone will get the same error (running 5.3.5f1).......
Answer by Purplepigfarm · Sep 19, 2016 at 03:43 PM
Still need help..... I don't know how to initialize my gameobject CarController
Your answer
![](https://koobas.hobune.stream/wayback/20220612092532im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with Error Code CS0019 2 Answers
Object reference not set to an instance of an object (c#) 0 Answers
ERROR Loading Script! WHAT'S WRONG?? 2 Answers
Bracket error 1 Answer