How do i save a modified level?

Hello people, i am quite new to programming in unity and i have done multiple searches on the internet and ended with a long method of saving my level, the game is a lego based game like minecraft. the method i am using (prepare your reading glasses and a cup of coffie) is:

private	List<Object> LegoBlocks = new List<Object>();
	private	List<Vector3> Positions = new List<Vector3>();
	private	List<Quaternion> Rotations = new List<Quaternion>();
	private	List<Object> LegoBlocks1 = new List<Object>();
	private	List<Vector3> Positions1 = new List<Vector3>();
	private	List<Quaternion> Rotations1 = new List<Quaternion>();
	private	List<Vector3> Spacings = new List<Vector3>();
	public int listlenght = 0;
	public GameObject[] block11;
	public GameObject[] block12;
	public GameObject[] block22;
	public GameObject[] blockplate11;
	public GameObject[] blockround11;
	public GameObject[] blockroundplate11;
	public GameObject[] blocklog11;
	public GameObject plant11;	
	public GameObject door134;
	public bool cansave = false;
	public bool canreset = false;
	public bool canload = false;


	// Use this for initialization
	void Awake () 
	{
		if (!Directory.Exists (@"C:\Users\Public\Documents\Saves")) {
			Directory.CreateDirectory (@"C:\Users\Public\Documents\Saves");
			StreamWriter writer = new StreamWriter (@"C:\Users\Public\Documents\Saves\TestSave.txt");
			writer.WriteLine ("Empty");
			writer.Close ();
				}
		else
		ReadFromFile();

	}

	void ObjectReciever(List<Object> blocks)
	{
		LegoBlocks = blocks;
	}

	void PositionReciever(List<Vector3> Poss)
	{
		Positions = Poss;
	}

	void RotationsReciever(List<Quaternion> rots)
	{
		Rotations = rots;
	}

	void SpacingReciever(List<Vector3> spaces)
	{
		Spacings = spaces;
	}
	// Update is called once per frame
	void Update () 
	{
		if (cansave == true) {
						
								LegoBlocks.AddRange (LegoBlocks1);
								Positions.AddRange (Positions1);
								Rotations.AddRange (Rotations1);
			LegoBlocks1.Clear();
			Positions1.Clear();
			Rotations1.Clear();
								SaveToFile ();
				cansave = false;
						
				}
		if (canload == true)
		{
					
								Application.LoadLevel (0);	
				canreset = false;

		}
		if (canreset == true)
		{
					
								LevelReset ();
				canload = false;

		}

	}

	void SaveScriptAllowed (bool save)
	{
		cansave = save;
	}

	void ResetScriptAllowed (bool reset)
	{
		canreset = reset;
	}

	void LoadScriptAllowed (bool load)
	{
		canload = load;
	}

	void LevelReset ()
	{
		StreamWriter writer = new StreamWriter (@"C:\Users\Public\Documents\Saves\TestSave.txt");
		writer.WriteLine ("Empty");
		writer.Close ();
	}

	void SaveToFile ()
	{
	
		if (Directory.Exists(@"C:\Users\Public\Documents\Saves"))
		{

			LevelReset ();
			StreamWriter writer = new StreamWriter (@"C:\Users\Public\Documents\Saves\TestSave.txt");
			listlenght = 0;
			foreach (Object o in LegoBlocks) {
				listlenght++;
			}
			string[] blocks = new string[listlenght];
			string[] pos = new string[listlenght];
			string[] rot = new string[listlenght];
			string[] space = new string[listlenght];
			int a = 0;
			foreach (Object o in LegoBlocks) {
				blocks [a] = o.ToString ();
				a++;
			}
			a = 0;
			foreach (Vector3 p in Positions) {
				pos [a] = p.ToString ();
				a++;
			}
			a = 0;
			foreach (Quaternion r in Rotations) {
				rot [a] = r.ToString ();
				a++;
			}
			
			for (int i = 0; i < listlenght; i++) {
				writer.WriteLine (blocks  <em>+ ":" + pos  _+ ":" + rot *);*_</em>

* }*
* writer.Close();*
* }*
* else*
* {*
* Directory.CreateDirectory(@“C:\Users\Public\Documents\Saves”);*
* SaveToFile(); *
* }*

* }*

* void ReadFromFile ()*
* {*

* StreamReader reader = new StreamReader (@“C:\Users\Public\Documents\Saves\TestSave.txt”);*
* string s = reader.ReadLine ();*
* if (s != “Empty”) {*
* while (s != null) {*
* char[] delimiter1 = {‘:’};*
* char[] delimiter2 = {‘,’};*
* string[] fields1 = s.Split (delimiter1);*
* string posstring1 = fields1 [1];*
* int t = posstring1.Length;*
* posstring1 = posstring1.Remove (t - 1);*
* posstring1 = posstring1.TrimStart (‘(’);*
* string[] posstring2 = posstring1.Split (delimiter2);*
* float[] pos1 = new float[3];*
* pos1 [0] = System.Convert.ToSingle (posstring2 [0]);*
* pos1 [1] = System.Convert.ToSingle (posstring2 [1]);*
* pos1 [2] = System.Convert.ToSingle (posstring2 [2]);*
* Vector3 pos = new Vector3 (pos1 [0], pos1 [1], pos1 [2]);*
* string rotstring1 = fields1 [2];*
* int r = rotstring1.Length;*
* rotstring1 = rotstring1.Remove (r - 1);*
* rotstring1 = rotstring1.TrimStart (‘(’);*
* string[] rotstring2 = rotstring1.Split (delimiter2);*
* float[] rot1 = new float[4];*
* rot1 [0] = System.Convert.ToSingle (rotstring2 [0]);*
* rot1 [1] = System.Convert.ToSingle (rotstring2 [1]);*
* rot1 [2] = System.Convert.ToSingle (rotstring2 [2]);*
* rot1 [3] = System.Convert.ToSingle (rotstring2 [3]);*
* Quaternion rot = new Quaternion (rot1 [0], rot1 [1], rot1 [2], rot1 [3]);*

* if (fields1 [0] == “1 1 Black(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue (Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Purple(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Black(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Blue(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Brown(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Green(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Pink(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Purple(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Red(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 White(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Yellow(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block12 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Black(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Blue(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Brown(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Green(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Pink(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Purple(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Red(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 White(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “2 2 Yellow(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (block22 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Black Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }if (fields1 [0] == “1 1 Purple Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockplate11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Black Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Purple Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow Round(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockround11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Black Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Blue Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Brown Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Green Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Pink Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Purple Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Red Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 White Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Yellow Round Plate(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blockroundplate11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Black Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [0], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Blue Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [1], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Brown Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [2], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Green Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [3], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Pink Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [4], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Purple Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [5], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Red Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [6], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 White Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [7], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 2 Yellow Log(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (blocklog11 [8], pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 1 Leave(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (plant11, pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*
* if (fields1 [0] == “1 3 4 Door(Clone) (UnityEngine.GameObject)”) {*
* LegoBlocks1.Add (Instantiate (door134, pos, rot));*
* Positions1.Add (pos);*
* Rotations1.Add (rot);*
* }*

* s = reader.ReadLine ();*
* }*
* }*
* }*
As you can see its a mess and i was wondering if there was an easier way to do this than having to save every block type individually and having to make new variables for each new block i put in, if anyone knows a better way to save a modified level then can you please help me, your help is appreciated.

One strategy could be to serialize your gameobjects. This link should help http://answers.unity3d.com/questions/476644/how-to-save-my-serialized-levels-.html

Take a look at whydoidoit.com too.

For such games it’s usually easier when you have a single array of prefabs, so it’s easier to access them in a generic way.

I would suggest to attach a simple script like this Block script to each of your Block prefabs:

public class Block : MonoBehaviour
{
    public int prefabIndex;
}

In your management script above you would only use a single array of “Block”'s and inside Awake you simply set the array index to every block:

public Block[] blocks; // assign your prefabs to this array in the inspector

void Awake()
{
    for(int i = 0; i < blocks.Length; i++)
        blocks*.prefabIndex = i;*

}
That way you can get the prefab index from each block directly when saving the scene. That means you would store that index in your file along with the position / rotation. When loading you just need to read / parse the integer value and use it as index into the blocks array to instantiate the object.
You also don’t need a Positions and Rotations List here. Each block knows it’s own position / rotation. Just use it’s transform.position and transform.rotation.
Your LegoBlocks list (the one that holds the block instances) should be declared like this:
private List LegoBlocks = new List();
Your save routine would be something like:
void SaveToFile ()
{
string path = @“C:\Users\Public\Documents\Saves”;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
if (!Directory.Exists(path)) // directory could not be created.
return;
}
using(StreamWriter writer = new StreamWriter (path+@“\TestSave.txt”))
{
foreach (Block o in LegoBlocks)
{
Vector3 pos = o.transform.position;
Vector3 rot = o.transform.eulerAngles;
string posStr = “”+pos.x+“,”+pos.y+“,”+pos.z;
string rotStr = “”+rot.x+“,”+rot.y+“,”+rot.z;
writer.WriteLine (o.prefabIndex + “:” + posStr + “:” + rotStr);
}
writer.Close();
}
}
To load you just need something like this:
Vector3 ReadVector3(string aText)
{
var data = aText.Split(‘,’);
if (data.Length < 3)
return Vector3.zero;
return new Vector3(float.Parse(data[0], float.Parse(data[1], float.Parse(data[2]));
}

void ReadFromFile()
{
using(StreamReader reader = new StreamReader (@“C:\Users\Public\Documents\Saves\TestSave.txt”))
{
string s = reader.ReadLine ();
if (s == “Empty”)
return;
LegoBlock.Clear();
while (s != null)
{
string[] data = s.Split(‘:’);
if (data.Length < 3)
continue; // misformatted line, skip
int prefab = int.Parse(data[0]);
Vector3 pos = ReadVector3(data[1]);
Vector3 rot = ReadVector3(data[2]);

Block inst = (Block)Instantiate(blocks[prefab],pos, Quaternion.Euler(rot));
LegoBlocks.Add(inst);
s = reader.ReadLine ();
}
}
}
Keep in mind that you can add more information to the Block script to categorize your blocks for your menus.
Some important points:
- Never rely on a certain format when using ToString on Vector3 / Quaternion / GameObject / … The string conversion is for debugging purposes and is not guaranteed to keep that format in future versions. It’s always better to manually create a certain format.
- When using IO streams always wrap them in a using statement.
- Never use such a hardcoded filepath. Different windows version have way different folders for user storage. For this you should use [Environment.GetFolderPath][1] and use one of [those values][2].
[1]: Environment.GetFolderPath Method (System) | Microsoft Learn
[2]: Environment.SpecialFolder Enum (System) | Microsoft Learn

This may be of help: Unity - Scripting API: EditorApplication.SaveScene