NullReferenceException: Object reference not set to an instance of an object

public class PlayerShootController : NetworkedBehaviour
{
    [SerializeField] private CinemachineVirtualCamera aimCamera;
    [SerializeField] private CinemachineVirtualCamera followCamera;
    [SerializeField] private float normalSensitivity = 1f;
    [SerializeField] private float aimSensitivity = 0.5f;
    [SerializeField] private LayerMask aimColliderLayerMask;
   
    private StarterAssetsInputs _inputs;
    private ThirdPersonController _thirdPersonController;
   
    private void Awake()
    {
        _inputs = GetComponent<StarterAssetsInputs>();
    }

    private void Update()
    {
        if (IsLocalPlayer)
        {
            if (_inputs.aim)
            {
                followCamera.gameObject.SetActive(false);
                aimCamera.gameObject.SetActive(true);
                _thirdPersonController.SetSensitivity(aimSensitivity); // Error here
            }
            else
            {
                followCamera.gameObject.SetActive(true);
                aimCamera.gameObject.SetActive(false);
                _thirdPersonController.SetSensitivity(normalSensitivity); // Error here
            }

            Vector2 screenCenter = new Vector2(Screen.width / 2f, Screen.height / 2f);
            Ray ray = Camera.main.ScreenPointToRay(screenCenter);
            if (Physics.Raycast(ray, out RaycastHit raycastHit, 999f, aimColliderLayerMask))
            {
           
            }
        }
    }
}

ThirdPersonController.cs

public float Sensitivity = 1f;

public void SetSensitivity(float newSensitivity)
{
   Sensitivity = newSensitivity;
}

private void CameraRotation()
{
   // if there is an input and camera position is not fixed
   if (_input.look.sqrMagnitude >= _threshold && !LockCameraPosition)
   {
      //Don't multiply mouse input by Time.deltaTime;
      float deltaTimeMultiplier = IsCurrentDeviceMouse ? 1.0f : Time.deltaTime;
    
      _cinemachineTargetYaw += _input.look.x * deltaTimeMultiplier * Sensitivity; // it is used here 
      _cinemachineTargetPitch += _input.look.y * deltaTimeMultiplier * Sensitivity;
   }

   // clamp our rotations so our values are limited 360 degrees
   _cinemachineTargetYaw = ClampAngle(_cinemachineTargetYaw, float.MinValue, float.MaxValue);
   _cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp);

   // Cinemachine will follow this target
   CinemachineCameraTarget.transform.rotation = Quaternion.Euler(_cinemachineTargetPitch + CameraAngleOverride, _cinemachineTargetYaw, 0.0f);
}

It was working for a while, but then i changed something in the code in other class, complied it and this wild error happend. Am I blind or missing something?

7992030--1026975--upload_2022-3-24_22-39-12.png

Third person controller is null. There’s a guide on fixing ObjectReference errors pinned to this sub-forum.

2 Likes

How to fix a NullReferenceException error

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

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

NullReferenceException: Object reference not set to an instance of an object
PlayerShootController.Update () (at Assets/Scripts/PlayerShootController.cs:34)

NullReferenceException: Object reference not set to an instance of an object
PlayerShootController.Update () (at Assets/Scripts/PlayerShootController.cs:40)

Oh yes i can see that now, thanks