Does ScriptableObject.CreateInstance save the instance to disk?

I think about using ScriptableObjects for pluggable AI states as shown in an Unity Tutorial for the tanks-project.
Since different AIs can have different targets I would like to copy the state template, but the documentation doesn't tell if this CreateInstance creates it only in the memory, or if this gets saved somewhere onto disk.

CreateInstance doesn't save the instance to disk, no. There's two ways in which ScriptableObjects become permanent assets:
- You explicitly turn them into assets by doing AssetDatabase.CreateAsset(path, so);
- You assign the SO to a field of some MonoBehaviour in a scene, and save that scene.

In the second case, the SO becomes part of the scene until no MonoBehaviours reference it anymore. It's also only an edit time thing, since you can only save scenes at edit time.

Note that calling Instantiate(so) was significantly faster than doing CreateInstance last time I checked, so if you want to copy ScriptableObjects, it's a lot better to do Instantiate and be done with it than doing CreateInstance and copying over fields.

2 Likes