Saving data

I have a lot of objects (planets to be precise) and each of them has a script attached to it with a lot of variables (planet type, resource type, buildings on the planet, level of buildings etc)how can i save and load all of that data?

Use XML to save and retrieve the data. Take a look of following code, you will get an idea.

	public void SaveElement(int iLevel , GameObject [] arrObjects)
	{
		alObjects.Clear();
		foreach(GameObject obj in arrObjects)
		{
			alObjects.Add(obj);
		}
		alObjects.TrimToSize();
		
		XmlDocument xmlDoc = new XmlDocument();
		XmlElement rootElement = xmlDoc.CreateElement("Level");
		rootElement.SetAttribute("levelNumber" , iLevel.ToString());
		rootElement.SetAttribute("numberOfBalls" , mGameManager.NumberOfBalls.ToString());
		rootElement.SetAttribute("numberOfGoals" ,  mGameManager.GoalsNeededToCompleteTheLevel.ToString());
		
		XmlElement objectElement = xmlDoc.CreateElement("Objects");
		
		foreach(GameObject obj in alObjects)
		{
			Vector3 obsPos = obj.transform.position;
			Quaternion obsRotation = obj.transform.localRotation;
			Vector3 obsScale = obj.transform.localScale;
			
			XmlElement element = xmlDoc.CreateElement("Object");
			element.SetAttribute("type",obj.name);
			element.SetAttribute("xPosition",obsPos.x.ToString());
			element.SetAttribute("yPosition",obsPos.y.ToString());
			element.SetAttribute("zPosition",obsPos.z.ToString());
			
			element.SetAttribute("xRotation",obsRotation.x.ToString());
			element.SetAttribute("yRotation",obsRotation.y.ToString());
			element.SetAttribute("zRotation",obsRotation.z.ToString());
			element.SetAttribute("wRotation",obsRotation.w.ToString());
			
			element.SetAttribute("xScale",obsScale.x.ToString());
			element.SetAttribute("yScale",obsScale.y.ToString());
			element.SetAttribute("zScale",obsScale.z.ToString());
			
			objectElement.AppendChild(element);
		}
		
		rootElement.AppendChild(objectElement);
		xmlDoc.AppendChild(rootElement);
		
		WriteXMLData(xmlDoc.InnerXml,"Level"+ iLevel +"Data.xml","MyGame");
	}

	/// <summary>
	/// Loads the level.
	/// </summary>
	/// <returns><c>true</c>, if level was loaded, <c>false</c> otherwise.</returns>
	/// <param name="iLevel">LevelNumber</param>
	public bool LoadLevel(int iLevel)
	{
		//*************
		mGameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
		//*************

		miLevelNumber = iLevel;
		bool bIsLevelExist = true;
		
		string strType;
		int iType = 0;
		Vector3 posObs ;
		Quaternion rotObs ;
		Vector3 scaObs ;
		
		string filePath;
		filePath = GetFilePath("Level"+ iLevel +"Data.xml","MyGame");
		
		try
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filePath);
			
			XmlNode root = xmlDoc.DocumentElement;
			mGameManager.NumberOfBalls = Convert.ToInt32(root.Attributes[1].Value);
			
			mGameManager.GoalsNeededToCompleteTheLevel = Convert.ToInt32(root.Attributes[2].Value);
			
			XmlNodeList nodeListObject = xmlDoc.GetElementsByTagName("Object");
			
			for(int i = 0; i < nodeListObject.Count ; i++)
			{
				strType = nodeListObject*.Attributes[0].Value;			*

_ float.TryParse( nodeListObject*.Attributes[1].Value ,out posObs.x );_
_ float.TryParse( nodeListObject.Attributes[2].Value ,out posObs.y );
float.TryParse( nodeListObject.Attributes[3].Value ,out posObs.z);*_

_ float.TryParse( nodeListObject*.Attributes[4].Value ,out rotObs.x );
float.TryParse( nodeListObject.Attributes[5].Value ,out rotObs.y );
float.TryParse( nodeListObject.Attributes[6].Value ,out rotObs.z );
float.TryParse( nodeListObject.Attributes[7].Value ,out rotObs.w );*_

_ float.TryParse( nodeListObject*.Attributes[8].Value ,out scaObs.x );
float.TryParse( nodeListObject.Attributes[9].Value ,out scaObs.y );
float.TryParse( nodeListObject.Attributes[10].Value ,out scaObs.z );*_

* InstantiateObject(posObs , rotObs , scaObs , strType);*
* }*

* }*
* catch(FileNotFoundException)*
* {*
* bIsLevelExist = false;*
* mGameManager.ResetValues();*
* mGameManager.LevelNumber = iLevel;*
* print (“Level FileNotFound !”);*
* }*
* return bIsLevelExist;*
* }*

* ///

*
* /// Writes the XML data to file.*
* ///
*
* /// Data*
* /// Filename*
* /// Folder name*
* public void WriteXMLData(string data , string filename, string GameName)*
* {*
* var FileLocation = Application.dataPath;
FileLocation = FileLocation + “/”;*
* var path = Application.dataPath.Substring (0, Application.dataPath.Length - 5);*

* path = path.Substring(0, path.LastIndexOf(‘/’));_
_FileLocation = path + “/Documents/GameXmlDocuments/”+GameName;*

* if(Directory.Exists(FileLocation))*
* {
}
else*

* {_
Directory.CreateDirectory(FileLocation);*
* }*_

* FileLocation =path + “/Documents/GameXmlDocuments/”+GameName+“/”;
File.WriteAllBytes(FileLocation+ filename ,ASCIIEncoding.ASCII.GetBytes(data));*
* }*

* ///

*
* /// Gets the file path.*
* ///
*
* /// The file path*
* /// File Name.*
* /// Folder name.*
* public string GetFilePath(string filename, string GameName)*
* {*
* var FileLocation = Application.dataPath;
FileLocation = FileLocation + “/”;*
* var path = Application.dataPath.Substring (0, Application.dataPath.Length - 5);*

* path = path.Substring(0, path.LastIndexOf(‘/’));*_

* _FileLocation = path + “/Documents/GameXmlDocuments/”+GameName+“/”;*

* return FileLocation+filename;*
* }*_

* ///

*
* /// Delete the level data file.*
* ///
*
* /// Level Number*
* public void CleanLevel(int iLevel)*
* {*
* mGameManager.DestroyAllObjects();*
* mGameManager.ResetValues();*
* mGameManager.LevelNumber = iLevel;*

* var FileLocation = Application.dataPath;
FileLocation = FileLocation + “/”;*
* var path = Application.dataPath.Substring (0, Application.dataPath.Length - 5);*

* path = path.Substring(0, path.LastIndexOf(‘/’));_
_FileLocation = path + “/Documents/GameXmlDocuments/MyGame”;*

* if(Directory.Exists(FileLocation))*
* {_
FileLocation =path + “/Documents/GameXmlDocuments/MyGame/” + “Level” + iLevel + “Data.xml”;
File.Delete(FileLocation);*
* }*

* }*

* #endregion*