Changing the position of an Game Object

How do I change the position of an Game Object?

transform.position

The above works if the script with that code is attached to the game object you want to move. If that’s not the case and the code is on object A and you want to move object B then:

  1. You’ll need a reference to the game object you want to move. You can do this by declaring a variable and setting the reference in the editor, or if you need to do it at run-time use GameObject.Find to find by name.

  2. Once you have a reference use ‘referenceVar.transform.’

  3. Note you can manually set the position or you can opt to translate it if you just want to move it from its current position by some amount.