Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. 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,975
    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,975
    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.