Saving Instantiated Objects

When weapons are dropped or loot is collected from a loot box, the objects are automatically instantiated and saved using the Save & Load add-ons. However, there may be times when you want to manually instantiate an object in your scene and ensure it’s saved, so that it appears the next time the game is loaded. Fortunately, the Save & Load Add-On makes this process simple and straightforward.

1

Identifiable Inheritance

Ensure your object to instantiate has a script that inherits from Identifiable, so the GameDataManager can recognize it.

using UnityEngine;
using cowsins;

public class ClassToSave : Identifiable 
{

}
2

Type To Prefab Mapping Assignment

Now, navigate to the Main Menu & locate DataPersistenceManager. This objects contains the GameDataManager component, where the Type To Prefab Mappings are stored. Create a new entry, select your CustomClassSaveData, and attach the Prefab to instantiate to it.

Check this guide for more related information.

3

Saving the Object

Now, you just need to instantiate the object, then access its Identifiable component, and call the following line:

identifiable.SaveInstance();

Here´s an example:

// Stores a reference of the instantiated object
GameObject instantiatedObj = Instantiate(objToInstantiate, transform.position, Quaternion.identity);
// Access Identifiable Component.
Identifiable identifiable = instantiatedObj.GetComponent<Identifiable>();

// Store instance.
identifiable.SaveInstance();

Last updated