- Home /
Swap Object Positions
I have two gameobjects that I want to swap positions. One object is offstage and the other moves around on stage. With a function call I want to swap the onstage object with the offstage object, but when I try swamping localposition and position, the positions are off.
What is the proper way to swap or toggle positions of a Gameobject?
Answer by Lovrenc · Jan 04, 2013 at 09:44 PM
Vector3 tempPosition = object1.transform.position;
object1.transform.position = object2.transform.position;
object2.transform.position = tempPosition;
Answer by Piflik · Jan 04, 2013 at 09:49 PM
I think this would be the easiest way to swap the position of two objects.
public GameObject other;
void Swap() {
Vector3 temp = transform.position;
transform.position = other.transform.position;
other.transform.position = temp;
}
Answer by JTAGames · Jun 25, 2021 at 06:38 AM
https://www.youtube.com/watch?v=GCXjPCRbspg
This is a tutorial that shows you how to do that, except it does it randomly but if you have just 2 game objects it should be fine.
Your answer
Follow this Question
Related Questions
Swapping gameobjects 2 Answers
What is the reasoning of multiplying by Time.deltaTime 1 Answer
does not work check if dead 1 Answer
Help around an approach to a "Virtual Motion Table" in Unity 0 Answers
Instantiated prefab position 5 Answers