# Baking (code generation)

To network entities through coherence, there needs to be a schema, and Unity scripts to communicate with the Replication Server. Baking takes care of that.

{% hint style="info" %}
Learn more about schemas in the [How does coherence work](/overview.md) section.
{% endhint %}

### How to Bake

Click on the **coherence > Bake** menu item.

This will go through all indexed `CoherenceSync` GameObjects (e.g. Resources folders) in the project and generate a schema file based on the selected variables, commands and other settings. It will also take into account any [LODs ](/manual/optimization/level-of-detail-lod.md)that have been added.

For every Prefab with a `CoherenceSync` component attached, the baking process will generate a C# baked script specifically tuned for it.

## Baking settings

Check [settings](/manual/project-settings.md).

When baking, the generated code will output to :file\_folder: `Assets/coherence/baked`.

* You can version the baked files or ignore them, your call.
  * If you work on a larger game or team, where you use continuous integration, chances are you are better off including the baked files on your VCS.
* Since baked scripts access your code, changing networked variables or commands with **coherence** will get you into compilation errors.

## Modifying networked variables or commands - The Watchdog

When you configure your Prefab to network variables, and then bake, **coherence** generates baked scripts that access your code directly, without using reflection. This means that whenever you change your code, you might break compilation by accident.

For example, if you have a `Health.cs` script which exposes a `public float health;` field, and you toggle `health` in the *Configure* window and bake, the generated baked script will access your component via its type, and your field via field name.

Like so:

```csharp
var healthComponent = GetComponent<Health>();
...
var healthField = healthComponent.health;
```

If you decide you want to change your component name (`Health`) or any of your bound fields (`health`), Unity script recompilation can fail. In this example, we will be removing `health` and adding `health2` in its place.

```csharp
//public float health;
public float health2;
```

When baking via assets, the watchdog is able to catch compilation problems related with this, and offer you a solution right away.

![](/files/Rydp3CUTKXDjv1Dy6NaP)

It will suggest that you delete the baked folder, and then diagnose the state of your Prefabs. After a few seconds of script recompilation, you will be presented with the *Diagnosis* window.

In this window, you can easily spot variables in your Prefabs that can't be resolved properly. In our example, `health` is no longer valid since we've moved it elsewhere (or deleted it).

From here, you can access the *Configure* window, where you can spot the problem.

![](/files/JgR1YngGUCUQa1H1E9JQ)

Now, we can manually rebind our data: unbind `health` and bind `health2`. Once we do, we can now safely bake again.

{% hint style="warning" %}
Remember to bake again after you fix your Prefabs.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coherence.io/manual/baking-and-code-generation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
