Assign HingeJoint.spring.targetPosition in C#

Hello

I get the following error : "Cannot modify a value type return value of `UnityEngine.HingeJoint.spring'. Consider storing the value in a temporary variable"

for this line of code: hingeJoint.spring.targetPosition = angle;

i know this problem from this codeline: transform.position.x += 5; but here the solution is transform.position += new Vector3(5, 0, 0);

But can i do in this case? Any idea?

I found the solution in case someone else needs it:

[quote=“andeeeee”, post:5, topic: 413053]
In C#, fields of a struct property can’t be set individually, rather you have to set the whole struct at once:-

JointSpring spr = hingeJoint.spring;
spr.targetPosition = 0;
hingeJoint.spring = spr;

[/quote]

2 Likes

[quote=“anon_94193701”, post:2, topic: 456311]
I found the solution in case someone else needs it:
[/quote]
Thank you VERY VERY much for posting your solution … this was a life-saver!!! Works great.