LogoLogo
HomeOnline DashboardAPIDiscordForums
SDK 1.7 Preview
SDK 1.7 Preview
  • Welcome
  • Overview
    • Features
    • Roadmap
  • Getting started
    • Get the Unity SDK
    • Setup a project
      • 1. Scene setup
      • 2. Prefab setup
      • 3. Test your game locally
        • Local testing using builds
        • Local testing via Unity's Multiplayer Play Mode
        • Local testing via ParrelSync
      • 4. Test in the cloud
        • Deploy a Replication Server
        • Share builds
    • How to... ?
    • Single-player to multiplayer
    • Video tutorials
    • Samples and tutorials
      • Package samples
      • Sample Connection UIs
      • First Steps tutorial
        • 1. Basic syncing
          • 1.1 Animation parameters
          • 1.2 Sending commands
        • 2. Physics / Authority transfer
        • 3. Areas of interest
        • 4. Parenting entities
        • 5. Complex hierarchies
        • 6. Persistence
      • Campfire project
        • Game mechanics
        • Leveraging object pooling
        • Remote interactions: Chairs
        • Remote interactions: Trees
        • A unique object with complex state
        • Custom instantiation and destruction
        • Running a server-side NPC
        • Playing audio and particles
        • A simple text chat
      • Beginner's guide to networking
    • Troubleshooting
  • Manual
    • Unity Components
      • CoherenceSync
      • CoherenceBridge
      • CoherenceLiveQuery
      • CoherenceTagQuery
      • CoherenceGlobalQuery
      • CoherenceInput
      • CoherenceNode
      • PrefabSyncGroup
      • Order of execution
    • Networking state changes
      • Instantiate and Destroy Objects
      • Supported types
      • Messaging with Commands
      • Syncing child GameObjects
      • Animation
      • CoherenceSync references
      • [Sync] and [Command] Attributes
      • [OnValueSynced] Attribute
      • Creating your own syncable member
      • Custom Component Actions
      • Rigid Bodies
      • Interpolation
    • Authority
      • Authority transfer
      • Server-authoritative setup
    • Lifetime
      • Persistence
      • Uniqueness
      • Example: A global counter
    • Parenting network entities
      • Direct children CoherenceSyncs
      • Deeply-nested CoherenceSyncs
      • Nesting Prefabs at Edit time
    • Asset management
      • Instantiating from CoherenceSyncConfig
      • Instantiate via
      • Load via
    • Scene management
    • Multiple Connections within a Game Instance
    • Baking (code generation)
      • Conditional compilation
    • Replication Server
      • Rooms and Worlds
      • Replication Server API
    • Simulators (Servers)
      • Scripting: Client vs Simulator
      • Run local Simulators
      • World Simulators
      • Room Simulators
      • Advanced Simulator Authority
      • Simulator slugs
      • Build and Deploy
      • Command-line arguments
    • Client Connections
    • Optimization
      • Areas of Interest
      • Level of Detail (LOD)
      • Profiling
      • Simulation Frequency
    • Project Settings
    • Advanced topics
      • Big worlds
        • World Origin Shifting
        • Load balancing
      • Competitive games
        • Simulation Frame
        • Determinism, Prediction and Rollback
      • Team workflows
        • Version Control integration
        • Continuous Integration
      • Schema explained
        • Specification
        • Field settings
        • Archetypes
      • Code stripping
      • Replication Server CLI
      • Single-player gameplay
    • Scripting API
  • Hosting
    • Choosing where to host
    • coherence Cloud
      • Online Dashboard
      • Manage Worlds
      • Configure Rooms
      • Player Accounts
      • Game Services
        • Lobbies
        • Cloud Storage
        • Key-Value Store (Legacy)
      • APIs
        • Worlds
        • Rooms
        • Lobbies
        • Cloud Storage
        • Key-Value Store (Legacy)
    • Peer-to-peer
      • Implementing Client hosting
        • Steam Relay
        • Epic Online Services (EOS) Relay
        • Azure PlayFab Relay
  • Support
    • Release notes
    • Glossary
    • Unreal Engine support
    • WebGL support
    • ECS / DOTS support
    • Known issues
    • Upgrade guide
      • Upgrade 1.6 -> 1.7
      • Upgrade 1.5 -> 1.6
      • Upgrade 1.4 -> 1.5
      • Upgrade 1.3 -> 1.4
      • Upgrade 1.2 -> 1.3
      • Upgrade 1.1 -> 1.2
      • Upgrade 1.0 -> 1.1
      • Upgrade 0.10 -> 1.0
      • Upgrade 0.9 -> 0.10
    • Credit cost & pricing
    • Report a bug
Powered by GitBook
On this page
  • Sync with coherence
  • Video Tutorial
  • Detailed Step by Step
  • 1. Add CoherenceSync component
  • 1a. To a new GameObject
  • 1b. To an already existing Prefab
  • 1c. Prefab variants
  • 2. Configure CoherenceSync
  • 2.1. Select variables to replicate
  • 3. Add a script
  • 4. Disable components on replicated objects
  • 5. Bake the netcode

Was this helpful?

Export as PDF
  1. Getting started
  2. Setup a project

2. Prefab setup

Preparing GameObjects and Prefabs for network replication

Was this helpful?

Sync with coherence

To start networking, simply select a Prefab or GameObject you wish to network, and on the Inspector window, click Sync with coherence.

Now, your Prefab should have a CoherenceSync component attached to it.

To select which properties you want to network, open the Configure window, accessible from within the CoherenceSync Inspector.

Video Tutorial

Setting up basic syncing is explained in this video, from 1:00 and onwards:

Detailed Step by Step

1. Add CoherenceSync component

For an object to be networked through coherence, it needs to have a CoherenceSync component attached, and be a Prefab.

Currently, only Prefabs can be networked.

1a. To a new GameObject

First, create a new GameObject. In this example, we're going to create a cube.

Next, we add the CoherenceSync component to Cube.

The CoherenceSync inspector now tells us that we need to make a Prefab out of this GameObject for it to work. We get to choose where to create it:

1b. To an already existing Prefab

Now you can either:

  • Click on the Sync with coherence checkbox at the top of the Prefab inspector.

  • Manually add the CoherenceSync component.

  • Drag the Prefab to the CoherenceSync Objects window. You can find it in coherence > CoherenceSync Objects.

1c. Prefab variants

One way to configure a pre-existing Prefab for networking, instead of just adding CoherenceSync to it, is to derive a Prefab variant and add the component to that instead.

In our Cube example, instead of adding CoherenceSync to Cube, you can create a Cube (Networked) Prefab and add the component to it:

This way, you can retain the original Prefab untouched, and build all the network functionality separately, in its own Prefab.

Another way to use Prefab variants to our advantage is to have a base Prefab using CoherenceSync, and create Prefab variants off that one with customizations.

For example, Enemy (base Prefab) and Enemy 1, Enemy 2, Enemy 3... (variant Prefabs, using different models, animations, materials, etc.). In this setup, all of the enemies will share base networking settings stored in CoherenceSync, so you don't have to manually update every one of them.

The Prefab variants inherits the network settings from their base, and you change those with overrides in the Configuration window. When a synced variable, method or component action is present in the variant and not in the parent, it will be bolded and it will have the blue line next to it, just like any other override in Unity:

2. Configure CoherenceSync

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

In its Inspector you can configure settings for Lifetime (Session-based or Persistent), Authority transfer (Not Transferable, Request or Steal), Simulate In (Client-side, Server-side or Server-side with Client Input) and Adoption settings for when persistent entities become orphaned, and more.

There are also a host of Events that are triggered at different times.

For now, we can leave these settings to their defaults.

2.1. Select variables to replicate

CoherenceSync allows you to automatically network all public variables and methods on any of the attached components, from built-in Unity components such as Transform, Animator , etc. to any custom script, including scripts that came with the Asset Store packages that you may have downloaded.

Make sure the variables you want to network are set to public. coherence cannot sync non-public variables.

To set it up, click on the Configure button in the CoherenceSync's Inspector. This brings up the Configuration window. Here you can select which variables you would like to sync across the network:

You will notice that position is already selected, and can't be unchecked. For our use case, let's also add rotation and localScale.

Close the Configuration dialog.

3. Add a script

Let's add a simple movement script that uses WASD or Arrow keys to move the Prefab in the scene.

Click on Assets > Create > C# Script. Name it Move.cs.

Copy-paste the following content into the file:

Move.cs
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);
    }
}

Wait for Unity to compile, then attach the script to the Prefab.

4. Disable components on replicated objects

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 with Prefab belonging to other Clients? We don't have authority over them, they just need to be replicated. We don't want our keyboard input interfering with them, we just want the position coming from the network to be replicated.

For this reason, we need the Move component to be disabled when the object is remote. coherence has a quick way to do this.

In the Configuration window, click the Components tab:

Here you will see a list of Component Actions that you can apply to non-authoritative entities that have been instantiated by coherence over the network.

Selecting Disable for your Move script will make sure the Component is disabled for network instances of the Prefab:

This ensures that if a copy of this Prefab is instantiated on a Client with no authority, this script will be disabled and won't interfere with the position that is being synced.

5. Bake the netcode

This process is very quick, and can be done in different ways:

  • From the menu item coherence > Bake

  • From the icon next to the coherence folder in the Project Window

  • From the Hub

  • From a CoherenceSync


To recap

This is it! Setting up an object to be networked doesn't require additional steps:

  • A Prefab with a CoherenceSync on it

  • Configuring what to sync in the Configure window

  • Disabling components on remote entities, in the Configure > Components tab

  • Baking the netcode

When new Networked Prefabs are included, or the properties to network are changed, you will need to and make sure the Replication Server (either local or the one in the Cloud) is using the schema generated afterwards.

The steps below all do this, but from different starting points: a new (1a), a (1b), or a (1c).

First, ensure you enter , as we don't want to add the component as an override.

Ensure you're in Isolation Prefab editing mode, not In Context. Read about .

Ensure you're in Isolation Prefab editing mode, not In Context. Read about .

Learn how to create and use Prefab variants in the .

Its Inspector has quite a number of settings. For more information on them, refer to .

Note that you can configure variables, methods and components not only on the root, but also .

Once everything is setup, you should ensure to run the process of : coherence will produce the necessary netcode (i.e. a bunch of C# scripts) to ensure that when the game is running and the Client connects, all of the properties and network messages you might have configured will correctly sync.

Now let's run this setup or using the .

Bake
Prefab mode
Prefab modes
Prefab modes
Unity Manual
the CoherenceSync page
on child GameObjects
Baking
locally
coherence cloud
GameObject
pre-existing Prefab
Prefab Variant
Networking an existing Prefab
Creating a variant of Cube
Prefab variant with a few overriden synced properties
CoherenceSync inspector
Configure Window