Null reference exception only in builds when changing Polyline thickness

Avatar
  • updated
  • Not a bug

Changing Polyline thickness in runtime works great in Editor play mode.
In build though I get a null ref exception.

My calling code:

var lineRenderer = polyLineObject.GetShapeRenderer() as Shapes.Polyline;
lineRenderer.Thickness = settings._thickness;


Stacktrace:

UnityEngine.EventSystems.ExecuteEvents.Execute | System.Exception: NullReferenceException
Stack:
Shapes.ShapeRenderer.VerifyComponents () (at <project>/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:236)
Shapes.ShapeRenderer.ApplyProperties () (at <project>Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:536)
Shapes.ShapeRenderer.SetFloatNow (System.Int32 prop, System.Single value) (at <project>/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:592)
Shapes.Polyline.set_Thickness (System.Single value) (at <project>/Shapes/Scripts/Runtime/Components/Polyline.cs:67)

Anything I am doing wrong here?

Notice:
I added all Shapes Shaders in "Always Included Shaders" section

Reporting a bug? please specify Unity version:
Unity 2022.1.18f
Reporting a bug? please specify Shapes version:
4.2.1
Reporting a bug? please specify Render Pipeline:
Built-in render pipeline
Pinned replies
Avatar
christian petry
  • Answer

Okay I can confirm that this was a mistake on my side.
Destroyed gameObjects, had references left on Shapes.Renderer and of course no MeshRenderer exists or can be created anymore.

Sorry for all of this! You can close this.

Avatar
Freya Holmér creator

that's, bizarre, the only way I can see this happen is if the internal bool initializedComponents is incorrectly set to true, but it's not serialized so I can't see how this would happen unless it's called at a very odd time. what's the context you're calling this in?

Avatar
christian petry

I let my users decide how thick lines should be visualized.


So from a UI slider -> settings -> signal to my shape line settings -> distribute to all created/used lines (observer).

Should all be called on the main thread.

Avatar
christian petry

Just saw in your code that you check if 'rnd' is created ('createdRnd') but there is nothing done with this information.
Is there something missing that should create a MeshRenderer if not exists?

EDIT: Oh sorry. Saw that this is added inside the code already

Avatar
christian petry

Some further information:
This happens only on Polylines. Lines, Discs and Rectangles seem to work as expected.

Avatar
Freya Holmér creator

hm, so, in ShapeRenderer.cs there's a method called VerifyComponents on line 228, this should be false if the components don't exist. if it's false, it will go into MakeSureComponentExists method at line 213 for both the mesh filter and the mesh renderer, but somehow, after this, the mesh renderer doesn't exist, which is, very odd, unless somehow

1. initializedComponents is incorrectly true, even though the components aren't initialized, or

2. the renderer was somehow destroyed or

3. MakeSureComponentExists fails to create the component or its null checks

but I can't see why this would only happen in builds, it's, super strange

is there an exception thrown earlier that breaks at some prior moment?

Avatar
christian petry

Ah I could reproduce the behaviour also in Editor.
I'm guessing the error is on my side. Could be an already destroyed object.
I'll investigate and give feedback once I found the problem.

Avatar
christian petry
  • Answer

Okay I can confirm that this was a mistake on my side.
Destroyed gameObjects, had references left on Shapes.Renderer and of course no MeshRenderer exists or can be created anymore.

Sorry for all of this! You can close this.

Avatar
Freya Holmér creator
  • Not a bug

glad it worked out, and thanks for investigating!