Save entire Transform objects with childs

Hello,

I would like to save some Transform objects in a file (for example XML) including the state of the child objects of them (enabled/disabled).

I haven’t got any idea how to realize this (so with saving the child objects and their states).

Thanks in advance for any helpful answer.

Sincerely MG

For starters, you need a save system. To create a save system you can use a basic skeleton for one here:

using System;
using System.IO;
using UnityEngine.Windows;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.UI;

public static class SaveLoadManager {

	public static void SavePlayer(Player player){

		BinaryFormatter bf = new BinaryFormatter();
		var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
		String dir = Application.persistentDataPath + "/saves";
		PlayerData data = new PlayerData (player);

		DirectoryInfo dir_i = new DirectoryInfo(dir);
		FileInfo[] info = dir_i.GetFiles("*.wyrm");
		foreach (FileInfo f in info) {
			Debug.Log (f);
		}

		FileStream stream = new FileStream (dir + "/player" + (info.Length + 1) + ".wyrm", FileMode.Create);

		bf.Serialize (stream, data);
		stream.Close ();
	}

	public static float[] LoadPlayer(){

		String dir = Application.persistentDataPath + "/saves";

		DirectoryInfo dir_i = new DirectoryInfo(dir);
		FileInfo[] info = dir_i.GetFiles("*.save");
		foreach (FileInfo f in info) {
			Debug.Log (f);
		}

		if (info.Length >= 1) {
			
			BinaryFormatter bf = new BinaryFormatter ();
			var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
			FileStream stream = new FileStream (dir + "/player" + (info.Length) + ".save", FileMode.Open);
			PlayerData data = bf.Deserialize (stream) as PlayerData;

			stream.Close ();
			return data.stats;
		} else {
			return null;
		}
	}
}

[Serializable] //serializable means it is able to be saved
public class PlayerData {

	public float[] stats;

	public PlayerData(Player player){
		stats = new float[5]; //create an empty array and use it to store values
		stats [0] = player.Level; //you can change these into whatever you want to save, but it must be either a float or int.
		stats [1] = player.Experience;
		stats [2] = player.transform.position.x;
		stats [3] = player.transform.position.y;
		stats [4] = player.transform.position.z;
	}
}

Now that you have a SaveLoadManager you must create a Save() and Load() method inside your Player script. If you want to save booleans as integers you can look up any tutorial on it, such as this: Stack Overflow Tutorial

public void Save(){ //compress the data into Binary using the SaveLoadManager
		Debug.Log ("Saving data (player)");

		SaveLoadManager.SavePlayer (this); //you do not need a reference to the SaveLoadManager as it is not MonoBehavior
	}
	public void Load(){ //load the data from the file and set the values accordingly
		Debug.Log ("Loading data (player)");

		float[] loadedStats = SaveLoadManager.LoadPlayer (); //grab the list of values that are returned from loading and set them into a new array

		Level = (int) loadedStats [0]; //level is an int, so you can save ints as floats and load them back into ints
		Experience = loadedStats [1];

		transform.position = new Vector3 (loadedStats[2],loadedStats[3],loadedStats[4]); //this is how you can save your exact player position

You can drag the transform object (the parent) and place it to the Project menu

Thanks for all the answers and sorry for my late response!

I fixed this using a bool List with the same index as the childs have.
So the states of the objects are saved in this list and loaded and set with starting the game.