# Prefab setup

In this section, we will learn how to prepare a prefab for network replication.

### **1.** Create a prefab and move it to Resources

Add an asset and create a *prefab* of it. Make sure the *prefab* is in a `Resources` folder in your Unity project.

Here is an example:

#### 1.1 Create a simple Cube in the scene

`GameObject -> 3D Object -> Cube`

#### 1.2 Convert the Cube into a prefab

Create a `Resources` folder in your Project. Drag the Cube into the Resources folder to turn it into a prefab.

<div align="left"><img src="https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-MYvG7OmGmB6UohO3RKl%2F-MYvGuyw-FsLi5Ll-agA%2Fimage.png?alt=media&#x26;token=44210db2-d52c-47e7-b207-d5e6572c945f" alt=""></div>

{% hint style="warning" %}
It's important that your **prefab** is in a ***Resources folder*** so that Unity can load it at runtime. This is a Unity requirement, more info [here](https://docs.unity3d.com/ScriptReference/Resources.Load.html).&#x20;
{% endhint %}

### **2. Add CoherenceSync to the prefab**

<div align="left"><img src="https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv0ksjqOy2jWVMwsg4f9k%2Fuploads%2F7LxhH6K3JdgE4CM5qvip%2FScreenshot%202021-12-22%20at%2016.05.53.png?alt=media&#x26;token=6bb9204e-7738-440b-9d99-21a56522bd4c" alt=""></div>

The `CoherenceSync` component will help you prepare an object for network synchronization during design time. It also exposes an API that allows us to manipulate the object during runtime.

`CoherenceSync` will query all public variables and methods on any of the attached components, for example Unity components such as `Transform`, `Animator` , etc.. This will include any custom scripts such as `PlayerInput` and even scripts that came with *Asset Store* packages you may have downloaded. &#x20;

{% hint style="info" %}
You can find out more about CoherenceSync [here](https://docs.coherence.io/0.7.4/api-reference/network-sdk/coherencesync).&#x20;
{% endhint %}

### 3. Select variables to replicate

Select which variables you would like to sync across the network. Initially, this will probably be the `Transform` settings; **position**, **rotation**, **scale**.

Under `Configure`, click `Select fields and methods`.

![](https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv0ksjqOy2jWVMwsg4f9k%2Fuploads%2FpP5OqQrSonULY1rCg4uD%2FScreenshot%202021-12-22%20at%2016.07.59.png?alt=media\&token=575c7ee4-9ef5-47b1-a27f-92157e55f2a9)

In the *Configuration* dialog, select *position, rotation and scale.*

![](https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv0ksjqOy2jWVMwsg4f9k%2Fuploads%2FGx8Qocn94m2O4pguijGb%2FScreenshot%202021-12-22%20at%2016.09.03.png?alt=media\&token=3254768b-5da2-45cf-82f2-b2108a3d735e)

Close the *Configuration* dialo&#x67;*.*

### **4. Add an input script**

This simple input script will use WASD or the Arrow keys to move the prefab around the scene.&#x20;

Click on `Assets -> Create -> C# Script`.

Name it `Move.cs`. Copy-paste the following content into the file.

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

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

public class Move : MonoBehaviour
{
    public float speed = 1f;

    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");
    
        var spf = speed * Time.deltaTime;

        transform.position += transform.forward * (v * spf);
        transform.position += transform.right * (h * spf);
    }
}

```

{% endcode %}

Wait for Unity to compile the file, then add it onto the prefab.

### 5. Disable Input on replicated object

We have added a `Move` script to the prefab. This means that if we just run the scene, we will be able to use the keyboard to move the object around.

But what happens on another client where this object is not authoritative, but rather replicated? We will want the position to be replicated over the network, without the keyboard input interfering with it.

Open the `Events` section in `CoherenceSync`. Add a new `On Network Instantiation` handler by clicking on the **plus sign** next to it.

![](https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv0ksjqOy2jWVMwsg4f9k%2Fuploads%2F29knfDQhUoN1JP56js7n%2FScreenshot%202021-12-22%20at%2016.32.01.png?alt=media\&token=9dd7fbb7-e735-4557-bf71-b60de4cdc0cb)

Pull the Cube prefab into the **Runtime Only / None (Object)** field.

Now click the dropdown **No Function** and select `Move -> bool enabled`.

<div align="left"><img src="https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fv0ksjqOy2jWVMwsg4f9k%2Fuploads%2FbRHwzKijOsnPlyuY1TSm%2FScreenshot%202021-12-22%20at%2016.33.12.png?alt=media&#x26;token=3648472c-aa24-46e6-b4f0-b87f145e9eb1" alt=""></div>

Leave the Boolean field unchecked.

<div align="left"><img src="https://2821114902-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MWd0ZPEK7vE9nkE0b7G%2F-MYvKC8nR2eFSsYq3lGX%2F-MYvL3GS429HvWIe6KIs%2Fimage.png?alt=media&#x26;token=b08b5c69-c2d3-4f39-9500-2de02126a5cf" alt=""></div>

### &#x36;**. Additional CoherenceSync settings**

From the `CoherenceSync` component you can configure settings for **Lifetime** (`Session-based` or `Persistent`, **Authority transfer** (`Request` or `Steal`), **Simulation model** (`Client Side`, `Server Side` or `Server Side with Client Input`) and **Adoption** settings for when local persistent entities are orphaned.&#x20;

You can find more information in the [SDK Fundamentals](https://docs.coherence.io/0.7.4/api-reference/network-sdk). \
\
There are also some **Events** that are triggered at different times.&#x20;

* `On Before Networked Instantiation` (before the network is instantiated)
* `On Networked Instantiation` (when the GameObject is instantiated)
* `On Networked Destruction` (when the GameObject  is destroyed)
* `On Authority Gained` (when authority over the GameObject is transferred to the local client)
* `On Authority Lost` (when authority over the GameObject is transferred to another client)
* `On After Authority Transfer Rejected` (when GameObject's Authority transfer was requested and denied).
* `On Input Simulator Connected` (when client with simulator is ready for Server-side with Client Input)
* `On Input Owner Assigned` (when InputOwner was changed is ready)

  ## Extras

  <div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p><a href="../resources/helper-scripts">Helper scripts</a> and samples can be found here. </p></div>
