Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing suspension spring

Discussion in 'Scripting' started by YodelYum, Sep 10, 2014.

  1. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    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.

    Code (CSharp):
    1. wheelObject.GetComponent<WheelCollider> ().suspensionSpring.spring = spring; //spring is stored in a float-varialel with a value of 1000.0f
    2.  
    When I try to do this I get this error:
    Code (CSharp):
    1. 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
     
  2. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Consider storing the value in a temporary variable

    Code (csharp):
    1. WheelCollider wheelCol = wheelObject.GetComponent<WheelCollider>();
    2. wheelCol.suspensionSpring.spring = spring;
    untested, but here's the principle.
     
    Last edited: Sep 11, 2014
  3. YodelYum

    YodelYum

    Joined:
    Apr 3, 2013
    Posts:
    44
    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:

    Code (CSharp):
    1. JointSpring suspensionSpring = new JointSpring ();
    2.                         suspensionSpring.spring = spring;          
    3.                         suspensionSpring.damper = springDamper;          
    4.                         suspensionSpring.targetPosition = 0.0F;
    5.                         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.
     
    TurboCobra23 and AzzTlh_Dev like this.