Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question Does ScriptableObject.CreateInstance save the instance to disk?

Discussion in 'Documentation' started by buc, Feb 18, 2023.

  1. buc

    buc

    Joined:
    Apr 22, 2015
    Posts:
    120
    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.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,104
    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.
     
    buc likes this.