`UnityEngine.WheelCollider.suspensionSpring'. Consider storing the value in a temporary variable

Help erro!!

    using UnityEngine;
    using System.Collections;
   
    public class CarEngine : MonoBehaviour {
   
    public WheelCollider FrontLeftWheel;
    public WheelCollider FrontRightWheel;
    public WheelCollider BackLeftWheel;
    public WheelCollider BackRightWheel;
    public WheelCollider LightCollider;
        public float SteerAngle;
        public float[] GearRatio;
    private int CurrentGear = 0;
    public float EngineTorque = 600.0f;
        public float MaxEngineRPM = 3000.0f;
        public float MinEngineRPM = 1000.0f;
        public float EngineRPM = 0.0f;
    public GameObject RedLight1;
    public GameObject RedLight2;
    public GameObject BackwardLight1;
    public GameObject BackwardLight2;
    private Color redbright;
    private Color reddark;
    public AudioSource horn;
    private static int speed;
    public GameObject Smoke1;
    public ParticleSystem Smoke1p;
    private Color SMwhite;
    private Color SMgrey;
   
    void  Start (){
           
            GetComponent<Rigidbody>().centerOfMass.y = -1.5f;
    }
        IEnumerator  BackParticle (){
        yield return new WaitForSeconds(2);
            Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMgrey);
    }
   
    void  Update (){
    //smoke particle effect
        if(Input.GetKeyDown("w"))
        {
                Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMwhite);
            Smoke1p.startSize = 2.5f;
            BackParticle();
        }
       
        if(Input.GetKeyUp("w"))
        {
                Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMgrey);
            Smoke1p.startSize = 1;
        }
       
            speed = GetComponent<Rigidbody>().velocity.magnitude*2.5f;
            GetComponent<Rigidbody>().drag = GetComponent<Rigidbody>().velocity.magnitude / 250;
        EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
    //audio   
        ShiftGears();
            GetComponent<AudioSource>().volume = Mathf.Abs(MaxEngineRPM / EngineRPM) - 0.5f;
            GetComponent<AudioSource>().pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1;
       
            if ( GetComponent<AudioSource>().pitch > 1.5f ) 
        {
                GetComponent<AudioSource>().pitch -= 0.5f;
        }
    //backup for backward
        if((EngineRPM<=-200) && (speed>=25)){
        BackLeftWheel.brakeTorque = 30;
        BackRightWheel.brakeTorque = 30;
        EngineRPM=-190;
        }
        else if((EngineRPM<=-200) && (speed<=25)){
        BackLeftWheel.brakeTorque = 0;
        BackRightWheel.brakeTorque = 0;
        }
   
    //backup for friction car       
        if(speed>90)
        {
            FrontLeftWheel.suspensionSpring.spring = 1000;
            FrontRightWheel.suspensionSpring.spring = 1000;
            BackLeftWheel.suspensionSpring.spring = 1000;
            BackRightWheel.suspensionSpring.spring = 1000;
        }   
            if(speed<90)
            {
                FrontLeftWheel.suspensionSpring.spring = 2000;
                FrontRightWheel.suspensionSpring.spring = 2000;
                BackLeftWheel.suspensionSpring.spring = 2000;
                BackRightWheel.suspensionSpring.spring = 2000;
            }
                if(speed<70)
                {
                    FrontLeftWheel.suspensionSpring.spring = 2500;
                    FrontRightWheel.suspensionSpring.spring = 2500;
                    BackLeftWheel.suspensionSpring.spring = 2500;
                    BackRightWheel.suspensionSpring.spring = 2500;
                }   
                    if(speed<20)
                    {
                        FrontLeftWheel.suspensionSpring.spring = 5500;
                        FrontRightWheel.suspensionSpring.spring = 5500;
                        BackLeftWheel.suspensionSpring.spring = 5500;
                        BackRightWheel.suspensionSpring.spring = 5500;
                    }
       
    //value of steer if speed is higher steering is smaller.   
        if(speed<50)
        {
                SteerAngle = 30 + GetComponent<Rigidbody>().velocity.magnitude * -0.2f ;
        }
            if(speed>15)
            {
                SteerAngle = 15 + GetComponent<Rigidbody>().velocity.magnitude * -0.2f ;
            }
       
        if(Input.GetKeyDown("s"))
            {
                FrontLeftWheel.brakeTorque = 12;
                FrontRightWheel.brakeTorque = 12;
                RedLight1.GetComponent<Renderer>().material.color = redbright;
                RedLight2.GetComponent<Renderer>().material.color = redbright;
            }   
                if(Input.GetKeyUp("s"))
                    {
                        FrontLeftWheel.brakeTorque = 5;
                        FrontRightWheel.brakeTorque = 5;
                RedLight1.GetComponent<Renderer>().material.color = reddark;
                RedLight2.GetComponent<Renderer>().material.color = reddark;
                    }
    //handbrake   
        if(Input.GetKeyDown(KeyCode.Space))
            {   
                if(speed<15){
                BackLeftWheel.brakeTorque = 200;
                BackRightWheel.brakeTorque = 200;
                }
                BackLeftWheel.sidewaysFriction.extremumValue = 5;
                BackRightWheel.sidewaysFriction.extremumValue = 5;
                FrontLeftWheel.sidewaysFriction.extremumValue = 1000;
                FrontRightWheel.sidewaysFriction.extremumValue = 1000;           
            }
                if(Input.GetKeyUp(KeyCode.Space))
                    {
                        BackLeftWheel.brakeTorque = 0;
                        BackRightWheel.brakeTorque = 0;
                        BackLeftWheel.sidewaysFriction.extremumValue = 20000;
                        BackRightWheel.sidewaysFriction.extremumValue = 20000;
                        FrontLeftWheel.sidewaysFriction.extremumValue = 20000;
                        FrontRightWheel.sidewaysFriction.extremumValue = 20000;
                    }
   
    //lights detect
        if(EngineRPM<-2)
            {
                BackwardLight1.GetComponent<Renderer>().material.color = Color.white;
                BackwardLight2.GetComponent<Renderer>().material.color = Color.white;
                RedLight1.GetComponent<Renderer>().material.color = reddark;
                RedLight2.GetComponent<Renderer>().material.color = reddark;
            }
                if(EngineRPM>-1)
                    {
                BackwardLight1.GetComponent<Renderer>().material.color = Color.black;
                BackwardLight2.GetComponent<Renderer>().material.color = Color.black;
                    }
       
        FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
        FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
   
        FrontLeftWheel.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
        FrontRightWheel.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
        LightCollider.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
   
                if(Input.GetKeyDown("h"))
                    {
                        horn.Play();
                    }
    }
   
        void ShiftGears()
        {
            int AppropriateGear;
   
            // this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
            // the engine RPM fall within the desired range. The gear is then set to this "appropriate" value.
            if (EngineRPM >= MaxEngineRPM) {
                AppropriateGear = CurrentGear;
   
                for (int i = 0; i < GearRatio.Length; i++) {
                    if (FrontLeftWheel.rpm * GearRatio [i] < MaxEngineRPM) {
                        AppropriateGear = i;
                        break;
                    }
                }
   
                CurrentGear = AppropriateGear;
            }
       
            if (EngineRPM <= MinEngineRPM) {
                AppropriateGear = CurrentGear;
   
                for (int j = 0; j < GearRatio.Length; j--) {
                    if (FrontLeftWheel.rpm * GearRatio [j] > MinEngineRPM) {
                        AppropriateGear = j;
                        break;
                    }
                }       
                CurrentGear = AppropriateGear;
            }
        }   
    }

While I haven’t used .suspensionString myself, I assume this must be treated like altering transform.position.

JointSpring temp = FrontLeftWheel.suspensionSpring;
temp.spring = 1000;
FrontLeftWheel.suspensionSpring = temp;
1 Like

Can you help me with this problem too? Cannot modify a value type return value of `UnityEngine.Rigidbody.centerOfMass’. Consider storing the value in a temporary variable

This too please Cannot modify a value type return value of `UnityEngine.WheelCollider.sidewaysFriction’. Consider storing the value in a temporary variable Thank you if you can help me

These are all the exact same solution. Take the solution for the first one and figure out how to apply it to the others - it’s pretty straightforward.

What is happening when you see this error is that the accessor properties you’re using (.suspensionSpring, .centerOfMass) aren’t simple member variables, but rather, “properties” - a “Get” and “Set” function masquerading as a variable. They do this so that the values that they set can be validated (making sure they’re not negative maybe, for example) before they cause bigger issues.

What this means is that when you retrieve FrontLeftWheel.suspensionSpring, it’s giving you a copy, not a pointer to the original JointSpring. You can’t just set a value on the copy - it won’t work, and even if the compiler allowed you to set it, the original variable wouldn’t have changed. But what you can do is overwrite the JointSpring entirely.

Can your speech be too complicated? I’m Brazilian, and sometimes I do not understand much what you say.

Forget I already solved the problem:

Cannot modify a value type return value of `UnityEngine.Rigidbody.centerOfMass’. Consider storing the value in a temporary variable:

Cannot modify a value type return value of `UnityEngine.WheelCollider.sidewaysFriction’. Consider storing the value in a temporary variable :

WheelFrictionCurve myWfc;
myWfc = BackLeftWheel.sidewaysFriction;
myWfc.extremumSlip = 5;
BackLeftWheel.sidewaysFriction = myWfc;

1 Like