GameObject.transform.position on the y axis keeps changing even though object is stationary

Hello all,

I have a script that goes through all the objects with the tag “VehicleComponent” and then adds the object’s position, rotation and scale to lists storing information about the object. However, the float for the object’s position keeps changing to values near to the object’s actual y position (usually within 3 either way), but not the exact value. The object is always stationary and when I make each object print it’s position every frame, it shows that the object is indeed stationary.

Here is the relevant code:

  class VehicleComponents {
    		public enum Types {Cube, Wheel};
    		public static List<Vector3> localPosition = new List<Vector3> ();
    		public static List<Quaternion> localRotation = new List<Quaternion> ();
    		public static List<Vector3> localScale = new List<Vector3> ();
    		public static List<Types> type = new List<Types> ();
    	}

public void Save () {
		GameObject[] _vehicleComponents = GameObject.FindGameObjectsWithTag ("VehicleComponent");
		foreach (GameObject vehicleComponent in _vehicleComponents) {
			VehicleComponents.localPosition.Add (vehicleComponent.transform.position);
			VehicleComponents.localRotation.Add (vehicleComponent.transform.rotation);
			VehicleComponents.localScale.Add (vehicleComponent.transform.localScale);
			if (vehicleComponent.transform.name == "Cube(Clone)") {
				VehicleComponents.type.Add (VehicleComponents.Types.Cube);
			} else {
				VehicleComponents.type.Add (VehicleComponents.Types.Wheel);
			}
		}
	}

Does anyone have any suggestions of what might be the cause and how to fix the problem?

Thanks in advance

,Hey, it has been a year since the question was asked, but just in case for anyone else: I was having the same problem. Basically for me I was using Character Controller with its own gravity defined, and also added a rigid body that has the gravity option ticked. So even if the character controller was not moving the game object, the rigid body was causing the y value to decrease. So deleting the rigid body from the game object and just leaving the character controller solved it for me. Hope this helps :slight_smile: