Instantiating from CoherenceSyncConfig

This is somewhat of an advance technique. If you're new to coherence and simply looking to instantiate an object, refer to the Instantiation page.

Instead of hard referencing Prefabs in your scripts to instantiate them using Unity's own Instantiate(), you can reference a CoherenceSyncConfig and instantiate Prefab instances through our API. This will respect the custom loading and instantiation rules set on the CoherenceSync component, that is the instantiator specified in Instantiate via and the asset loader specified in the Load via properties.

In code terms, it utilizes the internal INetworkObjectProvider and INetworkObjectInstantiator interfaces to load and instantiate the Prefab in a given .

Reference to the CoherenceSyncConfig

To instantiate a networked Prefab starting from its CoherenceSyncConfig, you simply need to invoke GetInstance():

using Coherence.Toolkit;
using UnityEngine;

public class CoherenceSyncConfigExample : MonoBehaviour
{
    public CoherenceSyncConfig prefabSyncConfig;
    public CoherenceBridge bridge;
    
    private CoherenceSync instance1;
    private CoherenceSync instance2;
    
    void Start()
    {
        // Load the prefab and instantiate it in the current scene
        instance1 = prefabSyncConfig.GetInstance();
        // Load the prefab and instantiate it in the specific networked scene for the given CoherenceBridge
        instance2 = prefabSyncConfig.GetInstance(bridge, Vector3.zero, Quaternion.identity);
    }
    
    private void OnDestroy()
    {
        // Instances have to be destroyed via the ReleaseInstance method, so that its destruction is handle by the 
        // internal INetworkObjectInstantiator
        prefabSyncConfig.ReleaseInstance(instance1);
        prefabSyncConfig.ReleaseInstance(instance2);
    }
}

Reference to a Prefab ("hard reference")

You can also hard reference the Prefab in your script, and then use our CoherenceSync API to instantiate the Prefab:

using System;
using Coherence.Toolkit;
using UnityEngine;

public class InstantiatorViaPrefabReference : MonoBehaviour
{
    // Serialized Prefab reference
    public GameObject prefab;

    private CoherenceSync instance1;

    void Start()
    {
        CoherenceSync sync = prefab.GetComponent<CoherenceSync>();
        // Instantiate it in the current scene
        instance1 = sync.GetInstance();
    }

    private void OnDestroy()
    {
        // Instances have to be destroyed via the ReleaseInstance method, so that its destruction is handle by the 
        // internal INetworkObjectInstantiator
        instance1.ReleaseInstance();
    }
}

While the end result is the same, the main difference is that referencing a CoherenceSyncConfig instead of a Prefab makes it a much more scalable and future-proof architecture. Since it doesn't need a Prefab hard reference, you won't have to change the code if the way that the Prefab is loaded into memory changes later on (for example, if you go from Resources to load it via Addressables).

Last updated

Was this helpful?