CoherenceSync References
Entity references let you set up references between Entities and have those be synchronized, just like other value types (like integers, vectors, etc.)
To use Entity references, simply select any fields of type GameObject
, Transform
, or CoherenceSync
for syncing in the Configuration window:
![](https://docs.coherence.io/~gitbook/image?url=https%3A%2F%2F4229720839-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FdBiAEtFeZKdAwRyJ9MEL%252Fuploads%252Fve3RoGfbuVN5D2eibTF2%252FScreenshot%25202022-03-07%2520at%252017.30.10.png%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=7153830e&sv=1)
The synchronization works both when using reflection and in baked sync scripts.
Entity references can also be used as arguments in Commands.
Limitations
It's important to know about the situations when an Entity reference might become null, even though it seems like it should have a value:
A client might not have the referenced entity in its LiveQuery. A local reference can only be valid if there's an actual Entity instance to reference. If this becomes a problem, consider switching to using the CoherenceNode component or Parent-Child relationships of prefabs which ensures that another Entity becomes part of the query.
The owner of the Entity reference might sync the reference to the Replication Server before syncing the referenced Entity. This will lead to the Replication Server storing a null reference. If possible, try setting the Entity references during gameplay when the referenced Entities have already existed for a while.
In any case, it's important to use a defensive coding style when working with Entity references. Make sure that your code can handle missing Entities and nulls in a graceful way.
Last updated