ScriptableObjects

Persisting asset-backed state with PersistableScriptableObject, and why an ordinary asset has nothing stable to point at.

Unity NexusForge Persistence 0.2.0 Updated

Assets can be persisted; types cannot be#

A ScriptableObject is a project asset, not a scene object, and an ordinary asset has no stable identity in a save: its InstanceID changes between runs, and its type is not state. To make an asset persistable, derive it from PersistableScriptableObject, which gives it a persistent id.

csharp
using NexusForge.Persistence.Unity;

[CreateAssetMenu(menuName = class="tok-str">"MyGame/Game Settings")]
public sealed class GameSettings : PersistableScriptableObject
{
    public float musicVolume = 0.8f;
    public float sfxVolume = 1f;
    public bool subtitles;
}

Assign the asset into What to save and its fields are captured like any other object's, with the asset's persistent id as its identity.

Referencing assets from saved objects#

A reference to an asset is stored as the target's persistent id, never as the asset object:

csharp
public sealed class LevelConfig : MonoBehaviour
{
    public GameSettings settings;     // stored as settings' persistent id
    public GameObject spawnPoint;     // stored as spawnPoint's persistent id
}

That is what makes a save survive a project edit: the id stays with the asset while the object graph around it changes. Resolution back to a live object is a separate step with its own rules — see References.

Which ScriptableObject assets make sense to save#

CaseGuidance
Player-facing settings (volumes, subtitles, keybinds)Good fit: small, meaningful, and the player expects it to survive.
Editor-authored game design values (balance numbers)Usually not a save: those belong in the asset, which is already persisted by Unity.
Runtime-created assetsSupported if you persist their identity as well; they are not assets any more once the session ends.

Next#

Something wrong on this page? Every page here describes behaviour that is checked in the repository. If a page and the package disagree, the package wins.

Report a documentation problem · Frequently asked questions · Troubleshooting