Changing suspension spring

Hi there,

Im trying to attach and configure wheelColliders to my tank-model only with a C#-script.
So far everything went fine until I tried to set suspension spring and damper.

wheelObject.GetComponent<WheelCollider> ().suspensionSpring.spring = spring; //spring is stored in a float-varialel with a value of 1000.0f

When I try to do this I get this error:

Cannot modify a value type return value of `UnityEngine.WheelCollider.suspensionSpring'. Consider storing the value in a temporary variable

Isn’t it possible to change spring values or am I making a huge mistake there. The error-message doesnt really make sense to me.
Thanks in advance

Consider storing the value in a temporary variable

WheelCollider wheelCol = wheelObject.GetComponent<WheelCollider>();
wheelCol.suspensionSpring.spring = spring;

untested, but here’s the principle.

Well thanks for the reply, but you essentially did the same thing as I did.
You only split it off into two parts.

However I just found the way to do it. Here’s how:

JointSpring suspensionSpring = new JointSpring ();
                        suspensionSpring.spring = spring;           
                        suspensionSpring.damper = springDamper;           
                        suspensionSpring.targetPosition = 0.0F;
                        wheelObject.GetComponent<WheelCollider> ().suspensionSpring = suspensionSpring;

First you has to instantiate a JointSpring-object.
Then you have to attach this object to the suspensionSpring-attribute of the wheelCollider-object.
Directly manipulating the spring or damper-attribute doesnt work.

2 Likes