• RuntimesUnityBugs
  • Exception when Instantiating a Prefab contains SkeletonGraphic.

Problem statement

1) what you have tried

Create a prefab, contains a SkeletonGraphic and check 'Enable Separation' and 'Multiple CanvasRenderers'

When game is running, Instantiating that prefab.

2) what you expected,

Prefab instantiated.

3) what actually happened.

A exception is thrown.

NullReferenceException: Object reference not set to an instance of an object
Spine.Unity.SkeletonGraphic.UpdateMaterialsMultipleCanvasRenderers (Spine.Unity.SkeletonRendererInstruction currentInstructions) (at Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs:845)
Spine.Unity.SkeletonGraphic.UpdateMeshToInstructions () (at Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs:753)
Spine.Unity.SkeletonGraphic.Rebuild (UnityEngine.UI.CanvasUpdate update) (at Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs:317)
Spine.Unity.SkeletonGraphic.Awake () (at Assets/Spine/Runtime/spine-unity/Components/SkeletonGraphic.cs:298)
UnityEngine.Object:Instantiate(GameObject, Transform)
Test:Update() (at Assets/Test.cs:21)

Give step-by-step instructions so we can reproduce the problem, if possible.

Download the attached project, enter play mode, press A.

Runtime information

Tell us your Spine Runtimes version.

Package version: spine-unity-4.1-2023-07-19.unitypackage

Unity Version 2018.4.36f

Post your entire error messages here (or attach them), if you got any error messages.
spinebug.zip
8MB
Related Discussions
...

The issue you're encountering with the NullReferenceException when instantiating a prefab containing a SkeletonGraphic component with 'Enable Separation' and 'Multiple CanvasRenderers' checked is likely related to the specific version of the spine-unity runtime you are using. Here are some steps and considerations to help resolve the problem:

  1. Check Unity and Spine-Unity Versions: Ensure you are using compatible versions of Unity and the spine-unity runtime. The error might be due to a bug that has been fixed in a later version. You mentioned using spine-unity-4.1-2023-07-19.unitypackage with Unity 2018.4.36f. Consider updating to the latest spine-unity runtime if possible.

  2. Reproduction Steps:

    • Create a prefab containing a SkeletonGraphic component.
    • Enable 'Enable Separation' and 'Multiple CanvasRenderers' in the SkeletonGraphic component.
    • Instantiate the prefab during runtime.
  3. Potential Fixes:

  4. Provide Additional Information: If the issue persists, consider providing more details such as:

    • A minimal Unity project that reproduces the issue.
    • Screenshots of the SkeletonGraphic component settings in the Inspector, especially the 'Advanced' section.
    • Any additional error messages or logs from the Unity Console.

By following these steps, you should be able to identify and potentially resolve the issue with the NullReferenceException when instantiating the prefab.

After checking the source code, it seems that this exception is caused by the missing attachment of the separation slot.

Check the following code copied from MeshGenerator.cs, started from line 365, when slot has no attachment, noRender is set to true, then the material field will not be set.

Nevermind, using a slot with attachment still throws the same exception.


				if (noRender) {
					if (current.forceSeparate && generateMeshOverride) { // && current.rawVertexCount > 0) {
						{ // Add
							current.endSlot = i;
							current.preActiveClippingSlotSource = lastPreActiveClipping;

							workingSubmeshInstructions.Resize(submeshIndex + 1);
							workingSubmeshInstructions.Items[submeshIndex] = current;

							submeshIndex++;
						}

						current.startSlot = i;
						lastPreActiveClipping = clippingAttachmentSource;
#if SPINE_TRIANGLECHECK
						current.rawTriangleCount = 0;
						current.rawVertexCount = 0;
						current.rawFirstVertexIndex = totalRawVertexCount;
						current.hasClipping = clippingAttachmentSource >= 0;
#endif
					}
				} else {
#if !SPINE_TK2D
					Material material;
					if (isCustomSlotMaterialsPopulated) {
						if (!customSlotMaterials.TryGetValue(slot, out material))
							material = (Material)((AtlasRegion)region).page.rendererObject;
					} else {
						material = (Material)((AtlasRegion)region).page.rendererObject;
					}
#else
					// An AtlasRegion in plain spine-unity, spine-TK2D hooks into TK2D's system. eventual source of Material object.
					Material material = (region is Material) ? (Material)region : (Material)((AtlasRegion)region).page.rendererObject;
#endif

					if (current.forceSeparate || (current.rawVertexCount > 0 && !System.Object.ReferenceEquals(current.material, material))) { // Material changed. Add the previous submesh.
						{ // Add
							current.endSlot = i;
							current.preActiveClippingSlotSource = lastPreActiveClipping;

							workingSubmeshInstructions.Resize(submeshIndex + 1);
							workingSubmeshInstructions.Items[submeshIndex] = current;
							submeshIndex++;
						}
						current.startSlot = i;
						lastPreActiveClipping = clippingAttachmentSource;
#if SPINE_TRIANGLECHECK
						current.rawTriangleCount = 0;
						current.rawVertexCount = 0;
						current.rawFirstVertexIndex = totalRawVertexCount;
						current.hasClipping = clippingAttachmentSource >= 0;
#endif
					}

					// Update state for the next Attachment.
					current.material = material;
#if SPINE_TRIANGLECHECK
					current.rawTriangleCount += attachmentTriangleCount;
					current.rawVertexCount += attachmentVertexCount;
					current.rawFirstVertexIndex = totalRawVertexCount;
					totalRawVertexCount += attachmentVertexCount;
#endif
				}

Ok, I got this. The problem here is that my animation has no render slots before the separation slot. At least no render slots during the setup state. By adding two slots with attachments before and after the separation slot, no exception will be thrown anymore.

    ZumiKua Glad you've found a solution. This issue has already been fixed on later runtimes.