Search Unity

Question NullReferenceException

Discussion in 'Cinemachine' started by cind7750, Mar 19, 2022.

  1. cind7750

    cind7750

    Joined:
    Jun 20, 2021
    Posts:
    5
    Hello, I am trying to make the camera zoom out while travelling in trigger but only getting NullReferenceExceptions. I'm not sure what the problem is

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class OnTriggerZoomWalk : MonoBehaviour
    5. {
    6.     [SerializeField] CinemachineVirtualCamera vcam;
    7.  
    8.     private float startPos, startSize, newsize;
    9.     public float zoomOutMax, zoomStrength;
    10.     void Start()
    11.     {
    12.         vcam = GetComponent<CinemachineVirtualCamera>();
    13.     }
    14.  
    15.     private void OnTriggerEnter2D(Collider2D other) {
    16.         if(other.CompareTag("ZoomOutWalk"))
    17.         {
    18.             startPos = transform.position.x;
    19.             startSize = vcam.m_Lens.OrthographicSize;
    20.         }
    21.     }
    22.  
    23.     private void OnTriggerStay2D(Collider2D other)
    24.     {
    25.         if(other.CompareTag("ZoomOutWalk"))
    26.         {
    27.  
    28.             float currentSize = vcam.m_Lens.OrthographicSize;
    29.             newsize = (startPos - transform.position.x) * zoomStrength;
    30.             newsize = currentSize + newsize;
    31.             if(currentSize <= newsize && newsize <= zoomOutMax)
    32.             {
    33.                 vcam.m_Lens.OrthographicSize = newsize;
    34.             }
    35.         }
    36.        
    37.     }
    38.  
    39.     private void OnTriggerExit2D(Collider2D other)
    40.     {
    41.         vcam.m_Lens.OrthographicSize = startSize;
    42.     }
    43.  
    44. }
    45.  
    error messages;
    NullReferenceException: Object reference not set to an instance of an object.
    OnTriggerZoomWalk.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/scrips/OnTriggerZoomWalk.cs:21)
    NullReferenceException: Object reference not set to an instance of an object.
    OnTriggerZoomWalk.OnTriggerStay2D (UnityEngine.Collider2D other) (at Assets/scrips/OnTriggerZoomWalk.cs:30)
    NullReferenceException: Object reference not set to an instance of an object.
    OnTriggerZoomWalk.OnTriggerExit2D (UnityEngine.Collider2D other) (at Assets/scrips/OnTriggerZoomWalk.cs:43)
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    I would guess that vcam is null. On what object is this script added?
     
  3. cind7750

    cind7750

    Joined:
    Jun 20, 2021
    Posts:
    5
    Its on my player and here is what my inspector looks like
     

    Attached Files:

  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Your problem is here:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         vcam = GetComponent<CinemachineVirtualCamera>();
    4.     }
    This overwrites vcam with null because CinemachineVirtualCamera is not a component on player. Get rid of it.
     
  5. cind7750

    cind7750

    Joined:
    Jun 20, 2021
    Posts:
    5
    That solved the problem
    Thank you!
     
    Gregoryl likes this.