Store individual positions of GameObjects in a string

I have a scene where I can spawn objects, move them around, save their positions, move around some more, then load the saved positions.

When I spawn the objects I can see which ones go into the string in the Inspector. When I try to load the saved data, they all move to one position; the last position I made before saving.

How can I make them save and revert back to their individual positions rather than one?
Here is the script I’m using:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveScene : MonoBehaviour
{
    public GameObject[] objectComponents;

    public void Update()
    {
        // Find all GameObjects in scene with the tag "clone"
        // This is in Update function so it will continually search for clone GameObjects
      objectComponents = GameObject.FindGameObjectsWithTag("Clone");
    }
    public void SavePositions()
    {
        // Apply function to all GameObjects found in the string
        foreach (GameObject component in objectComponents)
        {
            if (component.activeInHierarchy)
            {
                // Save the x, y and z positions of the GameObjects
                PlayerPrefs.SetFloat("X", component.transform.position.x);
                PlayerPrefs.SetFloat("Y", component.transform.position.y);
                PlayerPrefs.SetFloat("Z", component.transform.position.z);
                print("Saved");
           }
        }
    }
    public void LoadPositions()
    {
        foreach (GameObject component in objectComponents)
        {
            if (component.activeInHierarchy)
            {
                // Apply the saved positions
                Vector3 change = component.transform.position;
                change.x = PlayerPrefs.GetFloat("X");
                change.y = PlayerPrefs.GetFloat("Y");
                change.z = PlayerPrefs.GetFloat("Z");
                component.transform.position = change;
                print("Loaded");
            }
        }
    }
}

Only the last GameObject in your array is saved since you are using the same name for each object.

If you have a different name for all the GameObjects you could for example do:

PlayerPrefs.SetFloat(component.name + "X", component.transform.position.x);
[...]
change.x = PlayerPrefs.GetFloat(component.name + "X");

The name you set must be unique, otherwise it will just be overwritten.

PlayerPrefs is saving (Key,Value). There can only be one key. You are always saving to keys “X”, “Y” and “Z”. The second object then overwrites the first, the third overwrites the second and so on. That’s why every object spawns at one place - the place of your last object, because previous keys were overwritten.

Your approach will thus only work when every object is of the same type and configuration. Then, instead of

PlayerPrefs.SetFloat("X", component.transform.position.x);

you could write

PlayerPrefs.SetFloat({UniqueID} + "X", component.transform.position.x);

so that newer keys are not overwritten. You need to create the UniqueID using a deterministic pattern, so you will be able to recreate them when loading. This will most likely not be the case though, so maybe you should look up “object serialization” and rethink your approach.

All of your objects use the same PlayerPrefs keys “X”, “Y” and “Z” to store their position. You can’t store 10 positions using 3 numbers.

You need a way to store their positions individually. One solution would be to just index them.

PlayerPrefs.SetInt("Count", objectComponents.Length);
int i = 0;
foreach (GameObject component in objectComponents)
{
    if (component.activeInHierarchy)
    {
        // Save the x, y and z positions of the GameObjects
        PlayerPrefs.SetFloat("X"+i, component.transform.position.x);
        PlayerPrefs.SetFloat("Y"+i, component.transform.position.y);
        PlayerPrefs.SetFloat("Z"+i, component.transform.position.z);
        i++;
     ...

I don’t know how exactly you want things to work but when loading, you should prepare for cases where you have saved more positions than there are objects or vice versa.

public void LoadPositions()
{
     int savedCount = PlayerPrefs.GetInt("Count");
     for (int i = 0; i < savedCount && i < objectComponents.Length;i++)
    {
        var component = objectComponents*;*

if (component.activeInHierarchy)
{
// Apply the saved positions
Vector3 change = component.transform.position;
change.x = PlayerPrefs.GetFloat(“X”+i);
change.y = PlayerPrefs.GetFloat(“Y”+i);
change.z = PlayerPrefs.GetFloat(“Z”+i);
There’s plenty of corner cases you might need to worry about too depending on your app. Like: does the same object need to end up in the same position after loading, or is it enough that some object ends up in each of the positions. In the former case your objects need some kind of an ID and you need to save and load them based on that ID, and once again worry about things like what if object “ABC” is in the scene but theres no position saved for it and vice versa.