NullReferenceException: Loaded <disc> material Ring is null

Avatar
  • updated
  • Fixed

We get an inconsistent error in console when using a prefab containing shapes that is serialized into another game object's

[SerializeField] public GameObject ThisPrefabContainsShapes;

Later, this is instantiated with GameObject.Instantiate(ThisPrefabContainsShapes)

I've try to isolate this a few times into a simpler case, but haven't been able to reproduce the issue. This is in play mode in the editor. Hoping that this might ring a bell, if not I can try again to isolate into a clearer bug case.

Exceptions are thrown for each shape component (disc, line, ring) in the prefab, stating that their material's being null, such as:


NullReferenceException: Loaded disc material Ring is null

Shapes.ShapesMaterialUtils.GetDiscMaterial (Shapes.DiscType type) (at Assets/Asset Packs/Shapes/Scripts/Runtime/Utils/ShapesMaterialUtils.cs:113)

Shapes.Disc.GetMaterials () (at Assets/Asset Packs/Shapes/Scripts/Runtime/Components/Disc.cs:256)

Shapes.ShapeRenderer.UpdateMaterial () (at Assets/Asset Packs/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:131)

Shapes.ShapeRenderer.InitializeProperties () (at Assets/Asset Packs/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:210)

Shapes.ShapeRenderer.OnValidate () (at Assets/Asset Packs/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:48)

UnityEditor.AssetDatabase:LoadAssetAtPath(String)

Shapes.ShapesMaterials:InitMaterial(String, String, String[]) (at Assets/Asset Packs/Shapes/Scripts/Runtime/Utils/ShapesMaterials.cs:39)

Shapes.ShapesMaterials:.ctor(String, String[]) (at Assets/Asset Packs/Shapes/Scripts/Runtime/Utils/ShapesMaterials.cs:23)

Shapes.ShapesMaterialUtils:.cctor() (at Assets/Asset Packs/Shapes/Scripts/Runtime/Utils/ShapesMaterialUtils.cs:49)

Shapes.ShapeRenderer:OnValidate() (at Assets/Asset Packs/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:48)

Reporting a bug? please specify Unity version:
2019.4.5f1, 2019.4.4f1
Reporting a bug? please specify Shapes version:
2.0.0
Reporting a bug? please specify Render Pipeline:
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Fixed

this should now be fixed in 2.3.2!

Avatar
Freya Holmér creator
  • Under Review

I've seen this happen before, but I've done some fixes since then. could you try and see if you can isolate this issue in 2.2.0?

Avatar
andrew trese

Just as an update, I am still getting it intermittently in 2.2.0. Thank you for the awesome update though :D

I will work on getting a simple isolation case for you to look at.

Avatar
Freya Holmér creator

thanks a ton andrew!

Avatar
sean w

Hi, let me know if I should file a separate thread for this, but I've been experiencing this same error, except it happens when I try to build and prevents the build from completing!

My Unity version is 2019.4.7f1 and my Shapes version is 2.2.0 :)

EDIT: Clicking "Shapes>Advanced>Regenerated Shaders and Materials" has fixed the issue and allowed the build to succeed. I will edit again if the null refs block building again and if I have any clues as to what's activating that!

Avatar
andrew trese

@sean - thanks for posting your solution. I've also used "Shapes>Advanced>Regenerate Shaders and Materials" and the copious errors OnValidate() have stopped. I'm now on 2019.4.9f1. 

EDIT: nevermind, they were gone for about 10 minutes, then Unity popped up an "importing" dialog in which a ton of Shapes assets were listed in the progress bar and now they are back again.

Also, it may not have been clear from the original post, all the shapes causing these errors OnValidate() work perfectly fine.

Avatar
andrew trese

Swapping over to not using serialized prefabs in [SerializeField] properties has seemed to resolve this. Now when we want to load shapes, we make sure we are doing it after the Awake/OnValidate chain is fully complete, and load them as needed with Instantiate(Resources.Load("path/shape prefab") as GameObject, pos, rot); and the OnValidate() errors are no  longer present.

Some kind of serialized field, script loading, race condition?

Avatar
Freya Holmér creator
  • Answer
  • Fixed

this should now be fixed in 2.3.2!

Avatar
andrew trese

Thanks for your dedication even in the absence of a reproducible case!