Search Unity

Bug CineMachine NullReferenceException at PostPipelineStageCallback

Discussion in 'Scripting' started by skynote07, Jun 1, 2023.

  1. skynote07

    skynote07

    Joined:
    May 10, 2022
    Posts:
    3
    Hello, currently I'm making a game with First Person View with Cinemachine with InputAction (new InputSystem) and currently whenever I compile a script I get a NullReferenceException error whenever I hover over the 'Stage' but the error disappears as soon as I start play until I edit a code. Is there possible solution to this?

    Below is the code where I believe the error happens 'if (stage == CinemachineCore.Stage.Aim)'

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class CineMachinePOVExtension : CinemachineExtension
    5. {
    6.     [SerializeField]
    7.     private float horizontalSpeed = 10f;
    8.     [SerializeField]
    9.     private float clampAngle = 90f;
    10.     private InputManager inputManager;
    11.     private Vector3 startingRotation;
    12.     protected override void Awake() {
    13.         if (startingRotation == null){startingRotation = transform.localRotation.eulerAngles;}
    14.         inputManager = InputManager.Instance;
    15.         base.Awake();
    16.     }
    17.    
    18.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    19.     {
    20.         if (vcam.Follow){
    21.             if(stage == CinemachineCore.Stage.Aim){
    22.                 Vector2 deltaInput = inputManager.GetMouseDelta();
    23.                 startingRotation.x += deltaInput.x * horizontalSpeed * Time.deltaTime;
    24.                 startingRotation.x = Mathf.Clamp(startingRotation.x, -clampAngle, clampAngle);
    25.                 state.RawOrientation = Quaternion.Euler(0f, startingRotation.x, 0f);
    26.             }
    27.         }
    28.     }
    29. }
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Same goes for any nullref:

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    In this case, since CM is calling you, just check for
    stage
    being null and don't use it, or perhaps immediately return.

    There may even be a discussion in the documentation for CM about expecting this. Or it might have a bug.

    Either way, same three steps always for nullref. :)
     
  3. skynote07

    skynote07

    Joined:
    May 10, 2022
    Posts:
    3
    Thanks I figured it out the problem was that InputManager was not present prior to starting the game. so I had to null check on InputManager
     
    Kurt-Dekker likes this.