# Get initial data

Accessing data at runtime can be done in several ways.

{% hint style="info" %}
Please note that you should **only modify runtime-DataObjects at runtime**. Those runtime-DataObjects are being serialized when saving to a file. See -> [Add data objects at runtime](add-data-objects-at-runtime)
{% endhint %}

## Get initial data by referencing the data object

The simplest way would be to reference your data object in your script so let’s assume we have an EnemyData object which looks like this:

```csharp
public class EnemyData : DataObject
{
    public int health;
}
```

Now, in our Monobehaviour script, we can add the following reference:

```csharp
public class Enemy : Monobehaviour
{
    // Our data object 
    public EnemyData enemyData;
}
```

You can now simply assign the EnemyData object by dragging the object from the Databrain editor to the Enemy script inspector field.

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FblKScwz88HunPQGkmggT%2Fassign.gif?alt=media&#x26;token=1b7b2a8d-1b21-42e6-9fb7-0724f0f81672" alt=""><figcaption></figcaption></figure></div>

### Using the dropdown attribute

<figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FarYUVqNSkxKiOxc8eZst%2Fobjectdropdown.gif?alt=media&#x26;token=c9dd135f-bf0a-479c-a59f-6ec37776051b" alt=""><figcaption></figcaption></figure>

{% content-ref url="../dataobject-property-drawer" %}
[dataobject-property-drawer](../dataobject-property-drawer)
{% endcontent-ref %}

By using the dropdown attribute, you can use additional features such as quickly unassigning an object or quickly creating a new DataObject of type EnemyData directly from the field. To do this, simply add the DropdownObjectAttribute to your EnemyData field like this:

```csharp
public class Enemy : Monobehaviour
{
    public DataLibrary data;

    // Our data object 
    [DataObjectDropdown("data")]
    public EnemyData enemyData;
}
```

{% hint style="warning" %}
Please note that we have to add an additional reference to our Data Library object and also have to pass in the name of the data library as parameter to the DataObjectDropdown attribute.&#x20;
{% endhint %}

We can now access the data directly from the data object like this:

```csharp
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.
        // Therefore we're registering a method to the initialization callback.
        data.RegisterInitializationCallback(Ready);     
    }
    
    // Data is ready
    void Ready()
    {
        // Get the initial health value
        var enemyHealth = enemyData.health;
    }
}
```

{% hint style="warning" %}
Please note that before we can access the data at runtime, we have to make sure the data is initialized. This can be done by registering a method to the initialization callback via **RegisterInitializationCallback**.
{% endhint %}

## Get initial data directly from the DataLibrary object

It is also possible to get the data object directly from the **DataLibrary** object.\
In this case you simply have to reference the DataLibrary object as follow:

```csharp
public class Enemy : MonoBehaviour
{
    public DataLibrary data;
}
```

You can now access the data object like this:

```csharp
public class Enemy : MonoBehaviour
{
    public DataLibrary data;
    
    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()
    {
        // Multiple ways for accessing data object at runtime
        
        // 1.
        // Get the enemy data object by Guid.
        // You can optionally pass the data type.
        var _enemyData1 = data.GetInitialDataObjectByGuid("", typeof(EnemyData))  as EnemyData;
        // Get health value;
        var _health = _enemyData1.health;
        
        // 2.
        // Get the enemy data object by its title.
        // In this case you have to make sure that the title is unique.
        // You can optionally pass the data type.
        var _enemyData2 = data.GetInitialDataObjectByTitle("Enemy", typeof(EnemyData))  as EnemyData;
          // Get health value;
        var _health = _enemyData1.health;
        
        // 3.
        // Get all data objects of the type
        var _allEnemies = GetAllInitialDataObjectsByType(typeof(EnemyData));
        // Iterate through all enemies
        for (int i = 0; i < _allEnemies.Count; i ++)
        {
            var _enemy = _allEnenmies[i] as EnemyData;
        }
    }
}
```

## API

Please head over to the API section of the DataLibrary to see more methods for accessing data from the DataLibrary object.

{% content-ref url="custom-gui" %}
[custom-gui](custom-gui)
{% endcontent-ref %}
