'cinemachine Error on trying to access Camera Distance in virtual camera

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.



Solution 1:[1]

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;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 phildev