unity runtime_error ai_generated true

MissingReferenceException: The sprite 'ui_button_bg' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.

ID: unity/missingreferenceexception-ui-image-sprite

Also available as: JSON · Markdown · 中文
82%Fix Rate
84%Confidence
1Evidence
2023-09-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Unity 2022.3.18f1 active
Unity 2023.1.5f1 active
Unity 2021.3.33f1 active

Root Cause

A UI Image component's sprite reference points to a Sprite object that was destroyed (e.g., via Resources.UnloadUnusedAssets or scene unloading) while the Image still exists.

generic

中文

UI Image 组件的精灵引用指向了一个已被销毁的 Sprite 对象(例如通过 Resources.UnloadUnusedAssets 或场景卸载),而 Image 仍然存在。

Official Documentation

https://docs.unity3d.com/ScriptReference/MissingReferenceException.html

Workarounds

  1. 85% success Before accessing the sprite, check if the Image component's sprite is null and reassign it from a persistent source (e.g., Resources.Load or Addressables).
    Before accessing the sprite, check if the Image component's sprite is null and reassign it from a persistent source (e.g., Resources.Load or Addressables).
  2. 80% success Ensure the sprite asset is marked as 'Keep' in the Addressables system or is included in a preloaded asset bundle to prevent it from being unloaded.
    Ensure the sprite asset is marked as 'Keep' in the Addressables system or is included in a preloaded asset bundle to prevent it from being unloaded.

中文步骤

  1. Before accessing the sprite, check if the Image component's sprite is null and reassign it from a persistent source (e.g., Resources.Load or Addressables).
  2. Ensure the sprite asset is marked as 'Keep' in the Addressables system or is included in a preloaded asset bundle to prevent it from being unloaded.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    The sprite reference is lost at runtime due to destruction, not because it was never assigned. Setting null prevents the error but breaks UI functionality.

  2. 95% fail

    This actually causes the problem; it destroys sprites that are still referenced.

  3. 90% fail

    Sprites are not GameObjects; DontDestroyOnLoad works on GameObjects, not assets.