Search Unity

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

Discussion in 'Scripting' started by ErikMartins, Feb 27, 2017.

  1. ErikMartins

    ErikMartins

    Joined:
    Oct 28, 2016
    Posts:
    49
    Help erro!!


    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.    
    4.     public class CarEngine : MonoBehaviour {
    5.    
    6.     public WheelCollider FrontLeftWheel;
    7.     public WheelCollider FrontRightWheel;
    8.     public WheelCollider BackLeftWheel;
    9.     public WheelCollider BackRightWheel;
    10.     public WheelCollider LightCollider;
    11.         public float SteerAngle;
    12.         public float[] GearRatio;
    13.     private int CurrentGear = 0;
    14.     public float EngineTorque = 600.0f;
    15.         public float MaxEngineRPM = 3000.0f;
    16.         public float MinEngineRPM = 1000.0f;
    17.         public float EngineRPM = 0.0f;
    18.     public GameObject RedLight1;
    19.     public GameObject RedLight2;
    20.     public GameObject BackwardLight1;
    21.     public GameObject BackwardLight2;
    22.     private Color redbright;
    23.     private Color reddark;
    24.     public AudioSource horn;
    25.     private static int speed;
    26.     public GameObject Smoke1;
    27.     public ParticleSystem Smoke1p;
    28.     private Color SMwhite;
    29.     private Color SMgrey;
    30.    
    31.     void  Start (){
    32.            
    33.             GetComponent<Rigidbody>().centerOfMass.y = -1.5f;
    34.     }
    35.         IEnumerator  BackParticle (){
    36.         yield return new WaitForSeconds(2);
    37.             Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMgrey);
    38.     }
    39.    
    40.     void  Update (){
    41.     //smoke particle effect
    42.         if(Input.GetKeyDown("w"))
    43.         {
    44.                 Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMwhite);
    45.             Smoke1p.startSize = 2.5f;
    46.             BackParticle();
    47.         }
    48.        
    49.         if(Input.GetKeyUp("w"))
    50.         {
    51.                 Smoke1.GetComponent<Renderer>().material.SetColor ("_TintColor", SMgrey);
    52.             Smoke1p.startSize = 1;
    53.         }
    54.        
    55.             speed = GetComponent<Rigidbody>().velocity.magnitude*2.5f;
    56.             GetComponent<Rigidbody>().drag = GetComponent<Rigidbody>().velocity.magnitude / 250;
    57.         EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
    58.     //audio  
    59.         ShiftGears();
    60.             GetComponent<AudioSource>().volume = Mathf.Abs(MaxEngineRPM / EngineRPM) - 0.5f;
    61.             GetComponent<AudioSource>().pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1;
    62.        
    63.             if ( GetComponent<AudioSource>().pitch > 1.5f )
    64.         {
    65.                 GetComponent<AudioSource>().pitch -= 0.5f;
    66.         }
    67.     //backup for backward
    68.         if((EngineRPM<=-200) && (speed>=25)){
    69.         BackLeftWheel.brakeTorque = 30;
    70.         BackRightWheel.brakeTorque = 30;
    71.         EngineRPM=-190;
    72.         }
    73.         else if((EngineRPM<=-200) && (speed<=25)){
    74.         BackLeftWheel.brakeTorque = 0;
    75.         BackRightWheel.brakeTorque = 0;
    76.         }
    77.    
    78.     //backup for friction car      
    79.         if(speed>90)
    80.         {
    81.             FrontLeftWheel.suspensionSpring.spring = 1000;
    82.             FrontRightWheel.suspensionSpring.spring = 1000;
    83.             BackLeftWheel.suspensionSpring.spring = 1000;
    84.             BackRightWheel.suspensionSpring.spring = 1000;
    85.         }  
    86.             if(speed<90)
    87.             {
    88.                 FrontLeftWheel.suspensionSpring.spring = 2000;
    89.                 FrontRightWheel.suspensionSpring.spring = 2000;
    90.                 BackLeftWheel.suspensionSpring.spring = 2000;
    91.                 BackRightWheel.suspensionSpring.spring = 2000;
    92.             }
    93.                 if(speed<70)
    94.                 {
    95.                     FrontLeftWheel.suspensionSpring.spring = 2500;
    96.                     FrontRightWheel.suspensionSpring.spring = 2500;
    97.                     BackLeftWheel.suspensionSpring.spring = 2500;
    98.                     BackRightWheel.suspensionSpring.spring = 2500;
    99.                 }  
    100.                     if(speed<20)
    101.                     {
    102.                         FrontLeftWheel.suspensionSpring.spring = 5500;
    103.                         FrontRightWheel.suspensionSpring.spring = 5500;
    104.                         BackLeftWheel.suspensionSpring.spring = 5500;
    105.                         BackRightWheel.suspensionSpring.spring = 5500;
    106.                     }
    107.        
    108.     //value of steer if speed is higher steering is smaller.  
    109.         if(speed<50)
    110.         {
    111.                 SteerAngle = 30 + GetComponent<Rigidbody>().velocity.magnitude * -0.2f ;
    112.         }
    113.             if(speed>15)
    114.             {
    115.                 SteerAngle = 15 + GetComponent<Rigidbody>().velocity.magnitude * -0.2f ;
    116.             }
    117.        
    118.         if(Input.GetKeyDown("s"))
    119.             {
    120.                 FrontLeftWheel.brakeTorque = 12;
    121.                 FrontRightWheel.brakeTorque = 12;
    122.                 RedLight1.GetComponent<Renderer>().material.color = redbright;
    123.                 RedLight2.GetComponent<Renderer>().material.color = redbright;
    124.             }  
    125.                 if(Input.GetKeyUp("s"))
    126.                     {
    127.                         FrontLeftWheel.brakeTorque = 5;
    128.                         FrontRightWheel.brakeTorque = 5;
    129.                 RedLight1.GetComponent<Renderer>().material.color = reddark;
    130.                 RedLight2.GetComponent<Renderer>().material.color = reddark;
    131.                     }
    132.     //handbrake  
    133.         if(Input.GetKeyDown(KeyCode.Space))
    134.             {  
    135.                 if(speed<15){
    136.                 BackLeftWheel.brakeTorque = 200;
    137.                 BackRightWheel.brakeTorque = 200;
    138.                 }
    139.                 BackLeftWheel.sidewaysFriction.extremumValue = 5;
    140.                 BackRightWheel.sidewaysFriction.extremumValue = 5;
    141.                 FrontLeftWheel.sidewaysFriction.extremumValue = 1000;
    142.                 FrontRightWheel.sidewaysFriction.extremumValue = 1000;          
    143.             }
    144.                 if(Input.GetKeyUp(KeyCode.Space))
    145.                     {
    146.                         BackLeftWheel.brakeTorque = 0;
    147.                         BackRightWheel.brakeTorque = 0;
    148.                         BackLeftWheel.sidewaysFriction.extremumValue = 20000;
    149.                         BackRightWheel.sidewaysFriction.extremumValue = 20000;
    150.                         FrontLeftWheel.sidewaysFriction.extremumValue = 20000;
    151.                         FrontRightWheel.sidewaysFriction.extremumValue = 20000;
    152.                     }
    153.    
    154.     //lights detect
    155.         if(EngineRPM<-2)
    156.             {
    157.                 BackwardLight1.GetComponent<Renderer>().material.color = Color.white;
    158.                 BackwardLight2.GetComponent<Renderer>().material.color = Color.white;
    159.                 RedLight1.GetComponent<Renderer>().material.color = reddark;
    160.                 RedLight2.GetComponent<Renderer>().material.color = reddark;
    161.             }
    162.                 if(EngineRPM>-1)
    163.                     {
    164.                 BackwardLight1.GetComponent<Renderer>().material.color = Color.black;
    165.                 BackwardLight2.GetComponent<Renderer>().material.color = Color.black;
    166.                     }
    167.        
    168.         FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
    169.         FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
    170.    
    171.         FrontLeftWheel.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
    172.         FrontRightWheel.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
    173.         LightCollider.steerAngle = SteerAngle * Input.GetAxis("Horizontal");
    174.    
    175.                 if(Input.GetKeyDown("h"))
    176.                     {
    177.                         horn.Play();
    178.                     }
    179.     }
    180.    
    181.         void ShiftGears()
    182.         {
    183.             int AppropriateGear;
    184.    
    185.             // this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
    186.             // the engine RPM fall within the desired range. The gear is then set to this "appropriate" value.
    187.             if (EngineRPM >= MaxEngineRPM) {
    188.                 AppropriateGear = CurrentGear;
    189.    
    190.                 for (int i = 0; i < GearRatio.Length; i++) {
    191.                     if (FrontLeftWheel.rpm * GearRatio [i] < MaxEngineRPM) {
    192.                         AppropriateGear = i;
    193.                         break;
    194.                     }
    195.                 }
    196.    
    197.                 CurrentGear = AppropriateGear;
    198.             }
    199.        
    200.             if (EngineRPM <= MinEngineRPM) {
    201.                 AppropriateGear = CurrentGear;
    202.    
    203.                 for (int j = 0; j < GearRatio.Length; j--) {
    204.                     if (FrontLeftWheel.rpm * GearRatio [j] > MinEngineRPM) {
    205.                         AppropriateGear = j;
    206.                         break;
    207.                     }
    208.                 }      
    209.                 CurrentGear = AppropriateGear;
    210.             }
    211.         }  
    212.     }
    213.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    While I haven't used .suspensionString myself, I assume this must be treated like altering transform.position.
    Code (csharp):
    1. JointSpring temp = FrontLeftWheel.suspensionSpring;
    2. temp.spring = 1000;
    3. FrontLeftWheel.suspensionSpring = temp;
     
    ErikMartins likes this.
  3. ErikMartins

    ErikMartins

    Joined:
    Oct 28, 2016
    Posts:
    49
    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
     
  4. ErikMartins

    ErikMartins

    Joined:
    Oct 28, 2016
    Posts:
    49
    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
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  6. ErikMartins

    ErikMartins

    Joined:
    Oct 28, 2016
    Posts:
    49
    Can your speech be too complicated? I'm Brazilian, and sometimes I do not understand much what you say.
     
  7. ErikMartins

    ErikMartins

    Joined:
    Oct 28, 2016
    Posts:
    49
    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:

    https://docs.unity3d.com/ScriptReference/Rigidbody-centerOfMass.html

    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;
     
    StarManta likes this.