r/UnityHelp • u/BuildingShowcase • 1h ago
GameObject.transform.position.x += 0.1f not doing anything
The title, even though position.x is a float, nothing appears to move, and no errors appear
Here is the code
Vector3 pressPos = presser.transform.position;
string DisplayNum = CounterNumber.ToString("D4");
for (int i = 0; i < 9; i += 1)
{
pressPos.x += 0.1f;
}
1
Upvotes
1
u/Pherexian55 39m ago
Vectors are copied as values not as a reference. You're setting pressPos equal to position, then changing pressPos again. You never actually changed the position, you just changed a variable you gave the same value as the position.
What you want to do is just use position instead of an intermediary variable.
1
u/ChaseCheetah 37m ago
Are you, later in the code, setting the presser.transform.position = pressPos?
Vector3 is a struct so it is passed by value, not reference, so you need to then update the original value with the updated one.