Search Unity

[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:
    5
    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:
    7,187
    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:
    5
    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:
    7,187
    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.
  5. Jrodz

    Jrodz

    Joined:
    Apr 8, 2014
    Posts:
    29
    Thanks for posting this guys... this indirectly helped me with a similar issue.

    FYI: id cache that call that assigns the controller variable and only set/unset controller when your input device actually changes instead of every frame.