# Order of execution

This page describes the order of various **coherence** events and scripts in relation to [Unity's main loop](https://docs.unity3d.com/Manual/ExecutionOrder.html).

## Script execution order

Check out [ScriptExecutionOrder](https://unityapi.coherence.io/docs/v1.0.0/api/Coherence.Toolkit.ScriptExecutionOrder.html).

Additionally, take a look at your project's **Script Execution Order settings** by opening *Edit > Project Settings* and selecting the *Script Execution Order* category. See this [Unity manual article](https://docs.unity3d.com/Manual/class-MonoManager.html) for more details.

{% hint style="warning" %}
Depending on the reason for a disconnection the `onDisconnected` event can be raised from different places in the code, including `LateUpdate`.
{% endhint %}

## CoherenceSync instance lifecycle <a href="#coherencesync-instance-lifecycle" id="coherencesync-instance-lifecycle"></a>

When a Prefab instance with CoherenceSync is created at runtime, it will be fully synchronized with the network in the OnEnable method of CoherenceSync. This means that you can expect your custom Components to have fully resolved synchronized values and authority state in your Awake method. It occurs in the following order:

#### CoherenceSync

1. Awake() is called
   1. Internal initialization.
2. OnEnable() is called
   1. Synchronize with a new or existing Network Entity.
   2. OnBeforeNetworkedInstantiation event is invoked.
   3. Initial component updates are applied (for entities you have no authority over).
   4. OnNetworkedInstantiation event is invoked.
   5. OnStateAuthority or OnStateRemote (for authority or non-authority instances respectively) event is invoked.

#### User-defined Components

1. Awake() is called
   1. At this point, if you get the CoherenceSync component, you can expect networked variables and authority state to be fully resolved.


---

# 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/1.7/manual/components/order-of-execution.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.
