Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Purplepigfarm · Sep 11, 2016 at 02:11 AM · scripting problemerror message

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Sep 10, 2016 at 09:26 PM 0
Share

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...

avatar image Purplepigfarm gjf · Sep 11, 2016 at 09:24 PM 0
Share

@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).......

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Purplepigfarm · Sep 19, 2016 at 03:43 PM

Still need help..... I don't know how to initialize my gameobject CarController

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with Error Code CS0019 2 Answers

All compiler errors have to be fixed, but I copy pasted the code from the tutorial roll a ball to ensure its right. It still gives me the error! 0 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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges