# Edit Code

Now that you are able to successfully stream the 3D model into Unity, it's time to make some custom adjustments.

Each asset will be instantiated with a script named `CustomBehaviour.cs` attached. You can edit this script to create any behavior you would like while referencing additional data streamed from the cloud.

## Code Example

From the project's packages folder, open the `/co.echo3d.unity/Runtime/CustomBehaviour.cs` script:

{% code title="CustomBehaviour.cs" %}

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomBehaviour : MonoBehaviour
{
    [HideInInspector]
    public Entry entry;

    /// <summary>
    /// EXAMPLE BEHAVIOUR
    /// Queries the database and names the object based on the result.
    /// </summary>

    // Use this for initialization
    void Start()
    {
        // Add RemoteTransformations script to object and set its entry
        this.gameObject.AddComponent<RemoteTransformations>().entry = entry;

        // ADD YOUR CODE HERE //
        // Qurey additional data to get the name
        string value = "";
        if (entry.getAdditionalData() != null && 
            entry.getAdditionalData().TryGetValue("name", out value))
        {
            // Set name
            this.gameObject.name = value;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}
```

{% endcode %}

{% hint style="info" %}
&#x20;Note that this is an regular Unity MonoBehaviour with additions to the Start() function.
{% endhint %}

Lines 18-19 attaches a `RemoteTransformations` component to the game object and sets its content entry. This component is in charge of enabling [real-time updates and animations](https://docs.echo3d.com/using-the-sdk#real-time-updates-and-animations).

```csharp
// Add RemoteTransformations script to object and set its entry
this.gameObject.AddComponent<RemoteTransformations>().entry = entry;
```

You can add any custom code after line 21. An example follows.

## Querying Metadata

Lines 22-28 are an example that queries the entry's metadata for a key called `name`, and if such key exists, set the game object's name to the corresponding value:

```csharp
string value = "";
if (entry.getAdditionalData() != null && 
    entry.getAdditionalData().TryGetValue("name", out value))
{
    // Set name
    this.gameObject.name = value;
}
```

Without the `name` key being set, the default game object's name is the asset filename.

![](https://3757500311-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M41BcmqhdFQ3r89wcIR%2F-M4HISkudm-tIc47Wykh%2F-M4HYHjE5Fq3wX77HsHp%2F26.1.gif?alt=media\&token=8287339a-bef0-4dfa-965f-19dff96c6f4e)

Use the console to [set a metadata](https://docs.echo3d.com/web-console/manage-pages/data-page/how-to-add-data#1-adding-a-data-entry-1) entry with the following data:

| key  | value         |
| ---- | ------------- |
| name | empire\_state |

{% hint style="info" %}
Built-in keywords will be suggested through a drop-down list but you can [add](https://docs.echo3d.com/web-console/manage-pages/data-page/how-to-add-data#1-adding-a-data-entry) **any key** and **any value** by typing it in the text input field.
{% endhint %}

Run Unity again and notice that the game object name automatically changes.

![](https://3757500311-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M41BcmqhdFQ3r89wcIR%2F-M4HISkudm-tIc47Wykh%2F-M4HXzJCLUahi5pb2VON%2F26.2.gif?alt=media\&token=45ff0bd1-49ef-4a59-829b-8d1c95e2e63b)

**Great work!** 🎉

## Posting Metadata

You can add metadata to the cloud or update existing metadata stored remotely by calling the `UpdateEntryData` function located in the `echo3D.cs` script. This function implements the [Post Metadata to an Entry](https://docs.echo3d.com/api/data#post-metadata-to-an-entry) API query.

In order to call this function from any other script you can use the Echo3DService instance to call`UpdateEntryData` function with this single line of code:

```csharp
Echo3DService.instance.UpdateEntryData("<ENTRY_ID>", "<DATA>", "<VALUE>");
```

Where `<ENTRY_ID>` is the a specific entry ID you are trying to post metadata too, `<DATA>` is the data key (e.g. `scale`), and `<VALUE>` is the data value (e.g. `2`).

## Subscribing for Metadata Changes

Add the following code to your `Start` function to register an action that will be executed when metadata is received from the cloud.

```csharp
// Define data action
WClient.On(WClient.EventType.DATA_POST_ENTRY.ToString(), (string message) => {
    
    // Parse data
    string[] messageArray = message.Split('|');
    string dataKey = messageArray[2];
    string dataValue = messageArray[3];
    
    // Add you code here
    // myFunction(dataKey, dataValue);
    
});
```
