Home / Unity / U04

Unity error: NullReferenceException: Object reference not set to an instance of an object / UnityEditor.SpriteInspector.BuildPreviewTexture (Int32 width, Int32 height, UnityEngine.Sprite sprite, UnityEngine.Material spriteRendererMaterial, Boolean isPolygon) (at C:/buildslave/unity/build/Editor/Mono/Inspector/SpriteFrameInspector.cs:254) — cause and fix

The prefab is in AssetBundle A, while its referenced sprite and SpriteAtlas are in another

ID
U04
Engine
Unity
Verification
原文命中(抓取页面)
Frequency
论坛点赞=0

Original message (verbatim)

NullReferenceException: Object reference not set to an instance of an object / UnityEditor.SpriteInspector.BuildPreviewTexture (Int32 width, Int32 height, UnityEngine.Sprite sprite, UnityEngine.Material spriteRendererMaterial, Boolean isPolygon) (at C:/buildslave/unity/build/Editor/Mono/Inspector/SpriteFrameInspector.cs:254)

Placeholders such as %s / %d are kept verbatim from the engine source and are not replaced with concrete values.

Trigger

The prefab is in AssetBundle A, while its referenced sprite and SpriteAtlas are in another AssetBundle B; after loading the sprite does not display, and the exception throws when clicking the sprite preview of that SpriteRenderer in the Inspector; the same scene may also show the main-thread assertion Assertion failed on expression: 'Thread::CurrentThreadIsMainThread()'. Multi-line exception stack.

Typical cause

On a cross-bundle reference the atlas texture is not loaded first, so the sprite's held atlas texture handle is null; when the Editor preview BuildPreviewTexture reads atlas texture pixels it dereferences the null object, causing NullReferenceException.

Step-by-step fix

  1. Before loading the scene/prefab, explicitly load the bundle containing the atlas with AssetBundle.LoadAssetAsync<SpriteAtlas> and register through the SpriteAtlasManager.atlasRequested callback, then load the bundle referencing the sprite.
  2. Or pack atlas and sprites into the same bundle with correct dependency order.

Source links

Per-entry sources and capture date are listed at /sources#U04.

Related errors

Related Unity errors

Need an offline quick-reference checklist?

Atlas error library Pro pack: 123 error references (110 single + 13 merged) plus a quick-fix guide, $19 one-time purchase, delivered as electronic files.

View Pro pack / order

Back to the all-errors index Checkout and buy the Pro pack — $19.00