Resetting moved object positions

Hello!

In my scene, I move an array of objects with transform.position = new Vector3 and wanted to know if there’s a way to return them back to their original position after I move them.

I was thinking I could try to save their initial positions in the start as some sort of variable then call that variable from a button press but I can’t figure out how to do that with the positions in an array.

Thank you for reading!

That’s exactly how you would do it. Steps to succes:

  • make an array of Vector3 the size of how many objects are in the array
  • at Start() store their transform.position in each one
  • at restore, move the position back into transform.position

Yes and it would be fastest way I guess.

For better memory usage, this would be my choice if there are too many and not all of them change.

SaveClass

        private static List<Vector3> changedPositions;

        public static int SavePositions(Vector3 position)
        {
            changedPositions.Add(position);
            return changedPositions.Count - 1;
        }

        public static Vector3 RevertPosition(int order)
        {
            Vector3 savedPosition = changedPositions[order];
            changedPositions.RemoveAt(order);
            return savedPosition;
        }

Object scripts

        private int saveOrder = -1;
        private void SaveThis()
        {
            saveOrder = SaveClass.SavePositions(transform.position);
        }

        private void LoadThis()
        {
            if (saveOrder >= 0)
            {
                transform.position = SaveClass.RevertPosition(saveOrder);
                saveOrder = -1;
            }
        }

You could also go full-Unity and just attach a MonoBehaviour to the objects you want to restore, perhaps something called a Restorable.

At Start() they would note their original position (and orientation too if you like) in local class variables.

Later when you want to restore them all, do a FindObjectsOfType<Restorable>();, iterate them all and call their RestoreMe() method.

I assume you’re only doing this at signal points such as player death, not all the time randomly, so performance of a find would be irrelevant because it is basically a reload step.

Thank you both for your responses! I am aiming for something a bit more simple since this is a small project, but I’ll see what I can do with the examples you’ve given and told me

1 Like

So I’ve been trying to figure out the correct way to write the code based on the logic, but I’m still running into an error CS1061: ‘Vector3’ does not contain a definition for ‘transform’ and no accessible extension method ‘transform’ accepting a first argument of type ‘Vector3’ could be found (are you missing a using directive or an assembly reference?).

public GameObject[ ] allTomatoes;

public Vector3[ ] originalPosition;

// Start is called before the first frame update
void Start()
{

for(int i = 0; i < allTomatoes.Length; i++)
{
originalPosition_.transform.position = allTomatoes*.transform.position;_
_
}_
_
}*_

public void OriginTomatoPositions()
{
for (int i = 0; i < allTomatoes.Length; i++)
{
allTomatoes_.transform.position = originalPosition*.transform.position;*_

}
}

So first use code tags.

But second, read your code and say to yourself what you are doing with those assignments.

For instance this:

originalPosition.transform.position = allTomatoes.transform.position;

should almost certainly be written:

originalPosition[i] = allTomatoes[i].position;

The right side is what you are dereferencing (the i-th tomato and its position), the left side is the i-th Vector3, where you are storing its position.

And then your restore would be the opposite direction assignment.

You also should NOT make originalPosition public, and you MUST first allocate it in Start() or else you will get null reference.

// in start, make enough room to store all tomato positions:
originalPosition = new Vector3[ allTomatoes.Length];

You might want to save a lot of time at this stage and work through some tutorials, otherwise almost everything you’re doing will be mysterious and happenstance. Arrays and collections are a vital part of almost any game, so you wanna know how to handle those perfectly.

EDIT: if you look at my first post, those three steps are still exactly what I am outlining above: allocate, copy away, then copy back.

1 Like

Wow thank you! I knew I had to store it first on the left side. I have different code that moves the objects. With a button I wanted to reset their positions. I’ll make sure to make originalPosition private.