- Home /
Storing transforms from objects in Array
Hi, I'm just messing around with Arrays in JavaScript in Unity and I had a question I can't seem to find the answer to.
I have an array of GameObjects that I all move to the same specific position. Now I'm writing a function that returns them to their original position.
How would I store each GameObject's original position? And how would I call on those positions again?
Thanks in advance!
It's really only about positions and tried your solution, but I think I didn't handle it correctly. Should I make a Vector3 array or something? And how can I store them then?
Not sure about JavaScript since I'm using C# but it would be something like
GameObject[] objects = new GameObject[numObjects];
Vector3[] positions = new Vector3[numObjects];
// save the positions
for(int i = 0; i < numObjects; i++)
{
objects[i] = YourGameObject;
positions[i] = YourGameObject.transform.position;
}
// do your stuff
// reset the positions
for(int i = 0; i < numObjects; i++)
{
GameObject current = objects[i];
current.transform.position = positions[i];
}
Answer by hathol · May 15, 2012 at 10:14 AM
If you only care about positions, you could just store the transform.position in an array at the same index as the gameobject in your other array and assign that back when resetting the objects. If you need scale and rotation as well, I would probably build some sort of small custom struct to store all the information you need and then save that in the array.
Or if you want it a bit more fancy, you could always attach a small script to the gameobjects that stores the values and then use some sort of custom save() and reset() function that sets and reassigns them.