Can't set points on polyline

Avatar
  • updated
  • Answered

Why doesn't this work?  Seems I can't set point positions?


polyline.PolyPoints[0].point.z = 3f;

Reporting a bug? please specify Unity version:
2020.1.2
Reporting a bug? please specify Shapes version:
2.2
Reporting a bug? please specify Render Pipeline:
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Under Review

generally, when modifying polyline points, you'll either want to use the SetPointPosition() method, like you did

there is also a direct indexer on polyline itself, if you want to use that!

your line: polyline.PolyPoints[0].point.z = 3f; doesn't work, because C# doesn't work that way

PolylinePoint and Vector3 are both value types, which means when you access them, you're only modifying a copy, not a reference to the original data 

the proper way to modify points would be to reassign the struct, as well as using the indexer like this:

PolylinePoint p = polyline[0]; // p is a copy, not a reference

p.point = new Vector3( p.point.x, p.point.y, 3f );
polyline[0] = p; // assign back to polyline

as for the errors - are they disabled objects? seems similar to this issue


https://shapes.userecho.com/communities/1/topics/56-changing-color-while-disc-gameobjcet-is-not-active-gives-error

Avatar
Dan

I found polyLine.PolyPoints[0].point.Set();

However now I'm getting a new error.

NullReferenceException: Object reference not set to an instance of an object
Shapes.ShapeRenderer.ApplyProperties () (at Assets/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:254)
Shapes.ShapeRenderer.SetFloatNow (System.Int32 prop, System.Single value) (at Assets/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:273)
Shapes.Disc.set_Radius (System.Single value) (at Assets/Shapes/Scripts/Runtime/Components/Disc.cs:115)
BuildingUiNear.ResizeShapes () (at Assets/game/scripts/game/buildings/ui/BuildingUiNear.cs:45)

Avatar
Dan

The above error happened when doing this.

disc.Radius = 0.5f;

polyLine.PolyPoint[0].point.Set(0f, 0.5f, 3f);   doesn't seem to do anything.

Avatar
Dan

Can I not setup some shapes in the editor then change then by script?  If so why not?

Avatar
Dan

I found polyLine.SetPointPosition() which works.  Shouldn't there be documentation for this?!

Setting disc.Radius gives the following error.

NullReferenceException: Object reference not set to an instance of an object
Shapes.ShapeRenderer.ApplyProperties () (at Assets/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:254)
Shapes.ShapeRenderer.SetFloatNow (System.Int32 prop, System.Single value) (at Assets/Shapes/Scripts/Runtime/Components/ShapeRenderer.cs:273)
Shapes.Disc.set_Radius (System.Single value) (at Assets/Shapes/Scripts/Runtime/Components/Disc.cs:115)
BuildingUiNear.ResizeShapes () (at Assets/game/scripts/game/buildings/ui/BuildingUiNear.cs:42)

Avatar
Freya Holmér creator
  • Answer
  • Under Review

generally, when modifying polyline points, you'll either want to use the SetPointPosition() method, like you did

there is also a direct indexer on polyline itself, if you want to use that!

your line: polyline.PolyPoints[0].point.z = 3f; doesn't work, because C# doesn't work that way

PolylinePoint and Vector3 are both value types, which means when you access them, you're only modifying a copy, not a reference to the original data 

the proper way to modify points would be to reassign the struct, as well as using the indexer like this:

PolylinePoint p = polyline[0]; // p is a copy, not a reference

p.point = new Vector3( p.point.x, p.point.y, 3f );
polyline[0] = p; // assign back to polyline

as for the errors - are they disabled objects? seems similar to this issue


https://shapes.userecho.com/communities/1/topics/56-changing-color-while-disc-gameobjcet-is-not-active-gives-error

Avatar
Dan

Yes the disc that is causing the error is parented under a non active object.  I still want to be able to set the radius even though it is not active.  In this case it resizes in awake() and becomes active when the player is near.

Avatar
Freya Holmér creator
  • Answered

closing this then, as there's already a topic on the other issue! assuming setting your polyline points is working and all :)