Search Unity

Question Peculiar Null Reference Exception cause by Addressables

Discussion in 'Addressables' started by stroibot, May 18, 2022.

  1. stroibot

    stroibot

    Joined:
    Feb 15, 2017
    Posts:
    91
    So, I have a scrip that is attached to a Camera Game Object.
    Here's code:
    Code (CSharp):
    1.         private void Awake()
    2.         {
    3.             mainCamera = GetComponent<UnityEngine.Camera>();
    4.         }
    5.  
    6.         private void OnCameraZoom(float amount)
    7.         {
    8.             mainCamera.orthographicSize = Mathf.Clamp(mainCamera.orthographicSize - amount * Time.deltaTime, config.MinZoom, config.MaxZoom);
    9.         }
    10.  
    And here's funny thing - everything works fine in the Editor and in the Windows Build, but in Android build I get Null Reference exception, because `mainCamera` is null. Like, whaaaaat?

    While debugging I found this message (sadly enough logcat didn't gave me this message and I'm having troubles with building dev build with script debugging):
    MissingReferenceException: The object of type 'Camera' has been destroyed but you are still trying to access it.


    Ok... But what? How? Why does it work on Windows build and in the Editor?

    I've found a temporary solution, like using `Camera.main` if `mainCamera` is null, it works, but jeez.

    Is this some sort of bug?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,851
    Where exactly does addressables come into this?
     
  3. stroibot

    stroibot

    Joined:
    Feb 15, 2017
    Posts:
    91
    Because my game build around addressables. The camera game object is packed as addressable bundle and is loaded through scene reference. Or maybe it is android bug, I don't know.