0

I am trying to access to m_CameraDistance of the CinemachineVirtualCamera, but I get the error "NullReferenceException: Object reference not set to an instance of an object" on the last line of the code.

This is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Cinemachine;

    public class CameraDistanceController : MonoBehaviour
    {
        public float _areaCameraDistance = 10;
        private float _defaultAreaCameraDistance = 8;
        public CinemachineVirtualCamera _vcam;
        private CinemachineFramingTransposer _framingTransposer;

        void Start()
        {
            _framingTransposer = _vcam.GetCinemachineComponent<CinemachineFramingTransposer>();
            _defaultAreaCameraDistance = _framingTransposer.m_CameraDistance;
        }
    }

Any ideas? I found another person with the same issue, but there is no answer:

https://quabr.com/69938009/unity3d-cinemachine-how-to-access-camera-distance-in-virtual-camera

Thanks a lot.

Ken White
  • 123,280
  • 14
  • 225
  • 444
phildev
  • 15
  • 4
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ken White Apr 27 '22 at 03:19

1 Answers1

0

I am using _3rdPersonFollow, not CinemachineFramingTransposer. That is why.

See the fix below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CameraDistanceController : MonoBehaviour
{
    public float _areaCameraDistance = 10;
    private float _defaultAreaCameraDistance = 8;
    public CinemachineVirtualCamera _vcam;
    private Cinemachine3rdPersonFollow _3rdPersonFollow;

    void Start()
    {
        _3rdPersonFollow = _vcam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
        _defaultAreaCameraDistance = _3rdPersonFollow.CameraDistance;
    }
}
phildev
  • 15
  • 4