- Home /
Cant change transform.position of gameobject
I'm a beginner so I don't know if the answer is obvious or not. So I'm making a game and I have a coin GameObject and it is also assigned to a public static GameObject variable called coin1 in a script called GameStats. When I want to change the position of that GameObject from another script, I write: GameStats.coin1.transform.position = new Vector3(Random.Range(1, 4), Random.Range(-0.35f, 0.3f), 0.3f);
. In debug mode, the transform.position variable changes but not in the Unity scene. How do I fix this?
EDIT: I also tried doing:
GameStats.coin1.transform.position.Set(Random.Range(1, 4), Random.Range(-0.35f, 0.3f), 0.3f);
GameObject.Find("Coin").transform.position = new Vector3(Random.Range(1, 4), Random.Range(-0.35f, 0.3f), 0.3f);
And GameObject.Find("Coin").transform.position.Set(Random.Range(1, 4), Random.Range(-0.35f, 0.3f), 0.3f);
But none of them worked.
After thinking it could be that the variable is static so change it
It's still not working. I also tried making the GameObject variable in the same script it is used in, it detects collisions with the GameObject but it cant change its position for some reason.
Answer by yaminShihab · Mar 24, 2021 at 12:36 AM
Turns out I had a variable in a script attached to the coin GameObject and it was interfering with changing the position so I just changed the variable.