A Saving System for GameObjects

Hello i already have a saving system with the PlayerPrefs for the money etc. But i also need a script to save gameObjects that i spawned in the game while playing. I watched a lot of tutorials but no one helped me.

How can i save the objects easy? So that they not despawn when i left the game.

Thanks!

Its like an tycoon so at the start you have just a small house and then when you Upgrade it you have a Villa and the house Object gets destroyed. So i want to save the current building i have

Here’s an example script. It takes in a GameObject, which are all the different prefabs for each house. Then, it will spawn one based on the level you were last time. For example:

public GameObject[] Houses;
public int Level;

const string Key = "Level";

void Start()
{
     // Loads the saved level
     Level = PlayerPrefs.GetInt(Key, 1);

     // Spawns the correct house according to your level
     Instantiate(Houses[Level]);
}

public void Save()
{
     PlayerPrefs.SetInt(Key, Level);
}

@Pb2323

If you need a free, simple and open source save system you can try out this Persistence class. It’s very similar to PlayerPrefs but it’s main advantage over it is that you can write data to files of your choice and have as many of them as you want.

For example you can implement multiple save files per player profile, auto saves, etc. and leave PlayerPrefs for actual settings only.

Also, since it’s open source, you can customize it however you need. Implement binary read/write, compression or obfuscation method is a open option.
_
Here is an example. First you need some kind of a controller that makes Save and Load calls:

// PersistenceController.cs
using UnityEngine;
public class PersistenceController : MonoBehaviour
{
    public string directory = "my_game";
    public string fileName = "save001";
    void OnGUI ()
    {
        if( GUI.Button(new Rect(10,10,50,20),"Load")) Load();
        if( GUI.Button(new Rect(10,40,50,20),"Save")) Save();
    }
    public void Load () { Persistence.Load( directory , fileName ); }
    public void Save () { Persistence.Save( directory , fileName ); }
}

Then, implement saving and loading for your individual components or object pool managers:

// MyBuilding.cs
using UnityEngine;
public class MyBuilding : MonoBehaviour, Persistence.IMember
{
	[SerializeField] string ID = null;
	string Persistence.IMember.ID { get=>ID; set=>ID=value; }
	[SerializeField] bool _persistent = false;// set this to true in the inspector!
	bool Persistence.IMember.persistent { get=>_persistent; set=>_persistent=value; }

	
	[SerializeField] int _level = 0;
	string _key_level => $"{ID}.level";
	
	string _key_position => $"{ID}.position";


	void Awake ()
	{
		Persistence.ValidateID( this );

		Persistence.onAfterLoad += FromSaveData;
		Persistence.onBeforeSave += ToSaveData;
	}
	void OnDestroy ()
	{
		Persistence.onAfterLoad -= FromSaveData;
		Persistence.onBeforeSave -= ToSaveData;
	}

	/// <summary> Reads and applies save data </summary>
	/// <remarks> Save data -> game state </remarks>
	void FromSaveData ()
	{
		if( !_persistent ) return;

		if( Persistence.GetInteger(_key_level, out int getLevel) )
			_level = getLevel;
		
		if( Persistence.GetVector(_key_position, out Vector3 getPosition) )
			transform.position = getPosition;
	}
	/// <summary> Prepares save data for writing </summary>
	/// <remarks> Game state -> save data </remarks>
	void ToSaveData ()
	{
		if( !_persistent ) return;
		
		Persistence.SetInteger( _key_level , _level );
		Persistence.SetVector( _key_position , transform.position );
	}	
}