- Home /
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
Do you have any other objects with that tag in the scene that are moving? The line I am talking about is:
GameObject[] _vehicleComponents = GameObject.FindGameObjectsWithTag
("VehicleComponent");
Can you provide an example, with all digits, of the Y value you are seeing in Inspector and the Y value that's been 'changed'
Answer by leekenjen2328 · Jan 14, 2018 at 04:30 AM
,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 :)
Your answer
Follow this Question
Related Questions
Help with C# - destroy at a certain position not working 0 Answers
Any way to change transform.position of an indefinite number of gameobjects on FixedUpdate? 2 Answers
Distribute terrain in zones 3 Answers
Subtracting the position of transform from the position of Game Objects in a list. 1 Answer
Multiple Cars not working 1 Answer