Search Unity

Can't value of variables of scripts that are in an array

Discussion in 'Scripting' started by marceloapwf, Oct 28, 2020.

  1. marceloapwf

    marceloapwf

    Joined:
    Oct 12, 2020
    Posts:
    6
    The value of the variable suspensionForce does change in this script, but not in the objects that are linked to this array. It's working like a copy, but I don't want it to happen.

    dasdad.png

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.     public class ScrCarWheel : MonoBehaviour
    6.     {
    7.    
    8.    
    9.         [Header("Suspension")]
    10.         public Suspension[] wheelSusp;
    11.         public Vector3 forcaAntiRoll;
    12.    
    13.         void Start()
    14.         {
    15.         }
    16.    
    17.    
    18.         void FixedUpdate()
    19.         {
    20.    
    21.             float diferencaRoll = wheelSusp[0].springLength - wheelSusp[1].springLength;
    22.             float diferencaRollRear = wheelSusp[2].springLength - wheelSusp[3].springLength;
    23.    
    24.             foreach (Suspension susp in wheelSusp)
    25.             {
    26.                 if (susp.wheelFrontLeft)
    27.                 {        
    28.                     wheelSusp[0].suspensionForce = wheelSusp[0].suspensionForce - diferencaRoll * wheelSusp[1].springStiffness * wheelSusp[0].vetorNormal * (float)1000000;
    29.                 }
    30.                 if (susp.wheelFrontRight)
    31.                 {
    32.                     wheelSusp[1].suspensionForce = wheelSusp[1].suspensionForce + diferencaRoll * wheelSusp[0].springStiffness * wheelSusp[1].vetorNormal * (float)1000000;
    33.                 }
    34.                 if (susp.wheelRearLeft)
    35.                 {
    36.                     wheelSusp[2].suspensionForce = wheelSusp[2].suspensionForce - diferencaRollRear * wheelSusp[3].springStiffness * wheelSusp[2].vetorNormal * (float)1000000;
    37.                 }
    38.                 if (susp.wheelFrontLeft)
    39.                 {
    40.                     wheelSusp[3].suspensionForce = wheelSusp[3].suspensionForce + diferencaRollRear * wheelSusp[2].springStiffness * wheelSusp[3].vetorNormal * (float)1000000;
    41.                 }
    42.             }
    43.                
    44.     }
    45.    
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I think you need to look at the code that consumes the suspensionForce value. This is just setting it to something, but it doesn't seem like this is the script that actually uses the value. Perhaps the script that uses the value only reads it once and stores it locally, instead of continuous referring to it?
     
  3. marceloapwf

    marceloapwf

    Joined:
    Oct 12, 2020
    Posts:
    6
    suspensionForce is also calculated in the Suspension script. Can the problem be one script overwriting the other?
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Do you have two scripts setting the value of suspensionForce? Or do you have multiple copies of this script in the scene, all set to the same game objects?
     
  5. marceloapwf

    marceloapwf

    Joined:
    Oct 12, 2020
    Posts:
    6
    Two different scripts setting the value of suspensionForce.
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Then you probably shouldn't do that. Why are two things settings the value of a single property? Can't you just make sure the value is only being set once?