LogoLogo
⚠️ Outdated documentationGo to LatestHomeAPI
SDK 1.2
SDK 1.2
  • Welcome
  • Overview
    • How does coherence work?
    • Rooms, Worlds and Lobbies
    • Features and Roadmap
  • Learn
    • Beginner's guide to networking
    • Package samples
    • First Steps tutorial
      • 1. Basic syncing
        • 1.2. Animation parameters
        • 1.3. 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
    • How to network [genre]…
      • Racing
      • Turn-based
      • First-Person Shooter
      • MMO
      • Fighting
    • Video tutorials
  • Get started
    • Install coherence
    • Scene Setup
      • Sample UIs
    • Prefab setup: CoherenceSync
    • Local Development
      • Local Testing via Player Builds
      • Local Testing via Unity's Multiplayer Play Mode (MPPM)
      • Local Testing via ParrelSync
    • coherence Cloud
      • Create a free account
      • Deploy a Replication Server
      • Share builds
  • coherence SDK for Unity
    • Upgrade Guide
      • Upgrade 1.1 -> 1.2
      • Upgrade 1.0 -> 1.1
      • Upgrade 0.10 -> 1.0
      • Upgrade 0.9 -> 0.10
    • Components
      • CoherenceSync
      • CoherenceBridge
      • CoherenceLiveQuery
      • CoherenceTagQuery
      • Order of execution
    • Asset Management
      • Using CoherenceSyncConfig to instantiate GameObjects locally
    • Networking State Changes
      • Supported types
      • Messaging with Commands
      • Syncing child GameObjects
      • Parenting network entities
        • Direct child CoherenceSyncs
        • Deeply-nested CoherenceSyncs
        • Nesting Prefabs at Edit time
      • Animation
      • CoherenceSync references
      • [Sync] and [Command] Attributes
      • [OnValueSynced] Attribute
      • Creating your own syncable member
      • Custom Component Actions
    • Baking (code generation)
    • Scene Management
    • Authority
      • Authority transfer
      • Server-authoritative setup
    • Lifetime
      • Persistence
      • Uniqueness
      • Example – a global counter
    • Optimization
      • Simulation Frequency
      • Areas of Interest
      • Level of Detail (LOD)
    • Profiling
    • Interpolation
    • Rigid Bodies
    • Settings
    • Simulation Frame
    • Replication Server
    • Simulators
      • Scripting: Client vs Simulator
      • Local Development
      • World Simulators
      • Room Simulators
      • Simulator Slugs
      • Multi-Room Simulators
      • Build and Publish
      • Command-line arguments
      • Load Balancing
    • Client-Hosting
    • Client Connections
    • Rollback Networking Support
    • World Origin Shifting
    • Offline gameplay
    • CLI
    • Code Stripping
    • Version Control Integration
  • Hosting & Cloud
  • coherence Cloud
    • Developer Portal
    • Dashboard
    • Worlds
    • Rooms
    • Lobbies
    • Game Services
      • Account
      • Key-Value Store
    • Using coherence Cloud in Unity
      • Worlds
      • Rooms
      • Lobbies
      • Game Services
        • Authentication Service (Player Accounts)
        • Key-value store
  • Schema explained
    • Overview
    • Specification
    • Field settings
    • Archetypes
  • coherence API reference
  • Additional resources
    • Community
    • Continuous Integration
    • Unreal Engine Support
    • WebGL Support
    • Peer-to-Peer Support (P2P)
    • Glossary
  • Credit cost & pricing
  • Known Issues and Troubleshooting
  • Release Notes
Powered by GitBook
On this page
  • Nested Prefabs for instantiation
  • Nested Prefabs pre-placed in a Unity scene

Was this helpful?

Export as PDF
  1. coherence SDK for Unity
  2. Networking State Changes
  3. Parenting network entities

Nesting Prefabs at Edit time

Preparing nested connected Prefabs at edit time

Was this helpful?

coherence supports all Prefab-related Unity workflows, and nesting is one of them. It can make a lot of sense to prepare multiple networked Prefabs, parent them to each other, and either place them in the scene, or save them as a complex Prefab, ready to be instantiated. This page covers these cases.

Nested Prefabs for instantiation

When preparing a networked Prefab that contains another networked Prefab, one extra component is needed to allow coherence to sync the whole hierarchy: PrefabSyncGroup.

For instance, let's suppose we have a vehicle in an RTS that can carry cargo, and it comes with cargo pre-loaded when it's instantiated:

In this example Spacetruck is a synced Prefab, with 4 instances of the synced Prefab Cargo nested within. To make this work, we add a PrefabSyncedGroup to the root:

The component keeps track of child Prefabs that are also synced Prefabs. Now, whenever Spacetruck is instantiated, PrefabSyncGroup makes sure to take 4 instances of Cargo and link the Prefab instances to the correct network entities.

So to recap:

  • The outermost Prefab needs CoherenceSync and PrefabSyncGroup.

  • The child Prefabs need CoherenceSync and, optionally, CoherenceNode.

Nested Prefabs pre-placed in a Unity scene

When preparing such a Prefab, you need to set the Uniqueness property to No Duplicates. This ensures that, once multiple Clients connect and open the same scene, the synced Prefabs contained within are not spawned on the network multiple times.

Let's suppose we have a networked Prefab that represents a structure in an RTS (a LandingPad) that can be pre-placed in the scene. This structure also contains a networked vehicle Prefab (a Lander). This Prefab is synced as an independent network entity because at runtime it can detach, change ownership, be destroyed, etc.

To achieve this, all we need to do is ensure that both Prefabs are set to be unique. When we drag-and-drop the LandingPad Prefab into the scene, coherence automatically assigns a randomly-generated Prefab Instance Unique ID as an override. This number identifies these particular instances of these two Prefabs in the scene.

With this setting, we don't need to do anything else for these compound Prefabs to work.

To recap:

  • The outermost Prefab needs its Uniqueness set to No Duplicates. Optionally, you can add PrefabSyncGroup to enable runtime-instantiation.

  • Any child Prefab also needs its Uniqueness set to No Duplicates. It also needs a CoherenceNode if it's parented deep in the hierarchy.

Unique IDs on already-placed Prefabs

An important thing to keep in mind when working with compound Prefabs in the scene: when you add a new nested synced Prefab to an existing one that has already been placed in the scene a few times, the Prefab Instance Unique ID for these instances will initially be the same.

For this reason, once you play the game, you might see all children disappear (except one). That is normal: coherence thinks that all these network entities are the same, because they have the same uniqueness ID.

You need to ensure that these new children have an overriden and unique ID on each instance in the scene. To do so, click on the button next to the Prefab Instance Unique ID for each child that needs it:

Please note that if the nested Prefabs are more than one level under the root object, you still need to add a CoherenceNode component to the child ones (in the example above, Cargo), to enable .

When dealing with synced Prefabs that are hand-placed in the scene before connecting, such as level design elements like interactive doors, you need to ensure that they are seen as "unique". This is also covered in the , but it's worth talking about it in the context of nested synced Prefabs.

Like for runtime-instantiated Prefabs, keep in mind that if the Lander is nested in the hierarchy, it will also need a CoherenceNode component.

If you plan to also instantiate this Prefab at runtime, you can add a PrefabSyncGroup to the root as described in the . This makes the Prefab work when instantiated at runtime, while the uniqueness takes care of copies in the scene.

deep nesting at runtime
Uniqueness page
2 or more levels deep
previous section
Each instance received its own unique ID
Press the button to generate a new unique ID for this instance