Get/Set runtime data

Due to the way scriptable objects work, you can only modify runtime-DataObjects at runtime. Runtime changes to an initial scriptable object (non-runtime object) won't persist.

Get/Set runtime DataObject from initial DataObject

If your DataObject has been added to the runtime DataLibrary as described in (Add data objects at runtime) then you can access the runtime DataObject from the initial DataObject (which was created in the editor) using GetRuntimeDataObject. See the following code:

public class Enemy : MonoBehaviour
{
    public DataLibrary data;
    
    // Our data object
    [DataObjectDropdown("data")]   
    public EnemyData enemyData;
    
    public void Start()
    {
        // Before accessing the data we have to make sure the data library is ready
        data.RegisterInitializationCallback(Ready);     
    }
    
    // Data is ready
    void Ready()
    {
        // Get the runtime data object from the initial enemyData DataObject.      
        var _runtimeEnemyData = (enemyData.GetRuntimeDataObject() as EnemyData)
         // Modify the health value of the runtime data object.
        _runtimeEnemyData.health -= 10;
    }
}

Get/Set dynamic runtime DataObjects

The method CloneDataObjectToRuntime returns the newly created runtime data object. It is advised that you keep a reference of this dynamically added runtime data object to be able to modify its values.

Demo

Please also have a look at the demo scene in the Databrain package which demonstrates how to dynamically create DataObjects, change their values and restore the objects at runtime.

Last updated