Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[Solved] Script is causing "NullReferenceException" even though it's working fine?

Discussion in 'Scripting' started by PandaHead, May 18, 2018.

  1. PandaHead

    PandaHead

    Joined:
    Aug 31, 2017
    Posts:
    4
    So I have a camera rotation script using Cinemachine which works completely fine.

    So how come I'm getting a constant flooding of "NullReferenceException" errors in relation to the "freeLookCam" variable? It does it both with "Start()" and "Awake()" regardless and if I move the controller stick, the errors will be on line 24 and if the mouse, line 29. - Yet it works and the inspector says the correct game object for "freeLookCam" the whole time.

    Any ideas?

    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3. using InControl;
    4.  
    5. [RequireComponent(typeof(CinemachineFreeLook))]
    6.  
    7. public class CameraRotator : MonoBehaviour
    8. {
    9.     private InputDevice controller;
    10.     private CinemachineFreeLook freeLookCam;
    11.    
    12.     void Start()
    13.     {
    14.         freeLookCam = GetComponent<CinemachineFreeLook>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         controller = InputManager.ActiveDevice;
    20.  
    21.         // If controller input
    22.         if (controller.RightStickX)
    23.         {
    24.             freeLookCam.m_XAxis.m_InputAxisValue = controller.RightStickX.Value;
    25.         }
    26.         // If mouse input
    27.         else if (Input.GetAxis("Mouse X") != 0.0f)
    28.         {
    29.             freeLookCam.m_XAxis.m_InputAxisValue = Input.GetAxis("Mouse X");
    30.         }
    31.         // Otherwise, stop continuous camera rotation
    32.         else
    33.         {
    34.             freeLookCam.m_XAxis.m_InputAxisValue = 0f;
    35.         }
    36.        
    37.  
    38.     }
    39. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,891
    Are you sure you don't have duplicate copies of this script on accident in your scene?
     
    PandaHead likes this.
  3. PandaHead

    PandaHead

    Joined:
    Aug 31, 2017
    Posts:
    4
    I don't believe it, I did!
    I must have dragged it on something else by accident and not realised, thanks very much, such a simple thing! haha
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,891
    Glad it's fixed. Normally when I run into "I get errors, but everything works" that's the first thing I'll look for. Sometimes it happens when testing something. Other times it's a complete accident. I've also done it where I duplicated an object and forgot it had a script on it.
     
    PandaHead likes this.