Save a list of object locations

So i have a group of planets and i want to be able to save the game and when i load the game i want to load the planets at their previous locations…

My problem is that when i say to save the object location to playerprefs from the script it is loading each object into the same location because its the same script and saving to the same variable in playerprefs the only way to get around it that i can think of is to create a separate script for each planets relocation which sounds ridiculous and there must be a better way… so… what is it?

I thought about giving each object an ID or something but if i can’t generate playerpref vars in script it won’t matter.

You should look into JsonUtility, This way you can save the planets in this way:

//It needs to be serializable to become json
public class Planet
    //Same goes for variables
    public Vector3 location;

I recommend using System.IO to save/loading them from a planets folder like this.

private List<Planet> LoadPlanets()
    List<Planet> planets = new List<Planet>();
    if (Directory.Exists("Planets"))

        string[] info = Directory.GetFiles("Planets", "*.planet");
        foreach (string str in info)
    return planets;

private void SavePlanets(List<Planet> planets)
    if (!Directory.Exists("Planets"))
    foreach (Planet p in planets)
        File.WriteAllText("Planets/" +, JsonUtility.ToJson(p));

Hope this helps!!

The best and easiest way to save locations is to watch this video half way thru they start talking about serialization (which is almost as easy as playerprefs) if you follow the video step by step and listen to EVERYTHING he is saying… its an hour but it is so very worth it ~ by someone who has spent way too much time searching for a save/load solution.

Since you don’t have access to Unity’s “new” JsonUtility (which btw is quite limited) you can always use my SimpleJSON framework. You just need the “SimpleJSON.cs” file and the “SimpleJSONUnity.cs” file in your project. Given that you have all your planets in some kind of array and each planet has a unique name you can simply loop through them and do this:

void Save(GameObject[] planets)
    var data = new JSONObject();
    foreach (GameObject go in planets)
        data[]["position"] = go.transform.position;
        data[]["rotation"] = go.transform.rotation;
    PlayerPrefs.SetString("planets", data.ToString());

void Load(GameObject[] planets)
    if (!PlayerPrefs.HasKey("planets"))
    var data = JSON.Parse(PlayerPrefs.GetString("planets"));
    foreach (GameObject go in planets)
        go.transform.position = data[]["position"];
        go.transform.rotation = data[]["rotation"];

This would create a JSON text like this (though without the formatting):

    "Saturn": {
        "position" : [0, 123.456, 789],
        "rotation" : [0, 0, 0, 1],
    "Mars": {
        "position" : [555, -153, 852],
        "rotation" : [0, 0, 0, 1],

If you like the text to be nicely formatted like this you have to use

PlayerPrefs.SetString("planets", data.ToString(3));

instead of

PlayerPrefs.SetString("planets", data.ToString());

The “SimpleJSONUnity” file contains some convenient implicit conversion operators for most of Unity’s primitive types (Vector2/3/4, Quaternion, Matrix4x4, …)

here is how you could to it with a list:

//say this at the very top of your script!!!!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

// just fill your list and push s

	string path;
	string[] sa;
	string[] sa2;
	float[] loc;
	List<GameObject> planets = new List<GameObject>();

	int i;
	void Start () {	
		path = "c:/Save Info/";
		if (!Directory.Exists(path)) {Directory.CreateDirectory(path);	}
		path = path +"planets.txt";

		if (File.Exists (path)) {loc = new float[3];
			            sa2 = File.ReadAllText (path).Split ("-" [0]);
						sa = sa2*.Split ("," [0]);*
  •  				int i2  = 3; while (i2>0) {i2--;*
  •  				float.TryParse (sa [i2], out loc [i2]);}*

_ planets*.transform.position = new Vector3(loc[0],loc[1],loc[2]);}}}*_

* void Update () {*
* if (Input.GetKeyDown (“s”)) {*
* print(“saving”);*
* i = 0;string txt = “”;*
* while(i<planets.Count){*

* if(i!=0){txt = txt +“-”;}*
_ txt = txt+planets*.transform.position.x+

* i++; }*
* File.WriteAllText(path,txt);*

* }*