I store the positions of several gameobjects that are attached to another gameobject, and then instantiate them with those stored positions. I child them to the corresponding gameobject, and start the game paused, to avoid mini movements before completely loading.
But the objects I place slightly change position and rotation. If you made a text with the gameobjects on the surface, it will be unrecognizable after loading.
This is how I store the information:
void SaveGame()
{
for (int i = 1; i < amountOfBuildings + 1; i++) {
StrucValues = GameObject.Find (i.ToString ()).transform.position.ToString () +
GameObject.Find (i.ToString ()).transform.rotation.ToString ();
PlayerPrefs.SetString ("StrucStr" + i.ToString (), StrucValues);
}
PlayerPrefs.SetInt ("blueMinerals", BlueMinerals);
PlayerPrefs.SetInt ("redMinerals", RedMinerals);
string Lotoniapos = PlanetFocus[1].transform.position.ToString()+PlanetFocus[1].transform.rotation.ToString();
PlayerPrefs.SetString("Lotoniapos", Lotoniapos);
string Prutanikapos = PlanetFocus[2].transform.position.ToString()+PlanetFocus[2].transform.rotation.ToString();
PlayerPrefs.SetString("Prutanikapos", Prutanikapos);
string Rutospos = PlanetFocus[3].transform.position.ToString()+PlanetFocus[3].transform.rotation.ToString();
PlayerPrefs.SetString("Rutospos", Rutospos);
string Surinanpos = PlanetFocus[4].transform.position.ToString()+PlanetFocus[4].transform.rotation.ToString();
PlayerPrefs.SetString("Surinanpos", Surinanpos);
RotateAroundAsteroidSurinan[] asteroids = GameObject.FindObjectsOfType(typeof(RotateAroundAsteroidSurinan)) as RotateAroundAsteroidSurinan[];
for(int i = 0; i < asteroids.Length; i++)
{
string asteroidPosRot = asteroids_.transform.position.ToString ()+asteroids*.transform.rotation.ToString();*_
* PlayerPrefs.SetString (“Asteroidspos” + i.ToString (), asteroidPosRot);*
* }*
* PlayerPrefs.SetInt (“AmountOfBuildings”, amountOfBuildings);*
* PlayerPrefs.Save ();*
* }*
And this is how I instantiate it:
void LoadStructures(int i)
* {*
* string strucValues = PlayerPrefs.GetString (“StrucStr” + i.ToString ());*
* string prefabnPlanet = PlayerPrefs.GetString(“PrefabAndPlanet” + i.ToString ());*
* char[] delims = “(),”.ToCharArray ();*
* char[] delimsPP = “(),”.ToCharArray ();*
* string[] values = strucValues.Split (delims);*
* string[] valuesPP = prefabnPlanet.Split (delimsPP);*
* Vector3 pos;*
* Quaternion rot;*
* int whichplanet;*
* string whichprefab;*
* pos.x = float.Parse (values [1]);*
* pos.y = float.Parse (values [2]);*
* pos.z = float.Parse (values [3]);*
* rot.x = float.Parse (values [5]);*
* rot.y = float.Parse (values [6]);*
* rot.z = float.Parse (values [7]);*
* rot.w = float.Parse (values [8]);*
* whichplanet = int.Parse (valuesPP [1]);*
* whichprefab = valuesPP [3];*
* print ("Prefab: " + whichprefab);*
* sphereCollider = PlanetFocus[whichplanet].transform.GetComponent();*
* Buildings [whichPlanet] = (GameObject)Instantiate (Resources.Load (whichprefab), pos, rot);*
* Buildings [whichPlanet].transform.parent = PlanetFocus [whichplanet].transform;*
* //Buildings [whichPlanet].transform.LookAt (PlanetFocus[whichplanet].transform);*
* Buildings[whichPlanet].transform.name = i.ToString ();*
* PlayerPrefs.SetString(“PrefabAndPlanet” + amountOfBuildings.ToString (), “(” + whichplanet + “)” +*
* “(” + valuesPP[3] + “)”);*
* }*
Every time I create a structure, the prefab and planet number are stored in PlayerPrefs.
Does anyone know why the structures are loaded so inaccurately?
Thanks in advance.