SyncProperties
5 - 10 minutes read
In this section, you will use SyncProperty to update player's hp.
Why using SyncProperty
SyncProperties are designed for updating states that are not constantly changing.
Only send/receive when the property updates
Reliable
Supports serializable objects
Supports user-defined classes and structs.
Supports conflict resolution
SyncPropertyAgent
Select the Player Prefab. Click the Add Component button and Search "Agent", select Sync Property Agent to attach it to the Player GameObject.
Click the Add Sync Property button to create a SyncProperty for the Player. The Property's name is important and has to be unique in the same SyncPropertyAgent component. Set Name to "hp" and set Type to Int. The Conflict Resolution rule is default to Mine. You can use Mine for this tutorial. For more information on Conflict Resolution, please visit the following page.
Updating the PlayerHP.cs Script
Add two properties: networkId, and syncPropertyAgent.
You can remove the currentHP property as the player hp will be stored in the "hp" SyncProperty.
In the Start() method, and initialize the networkId, and syncPropertyAgent
Handling the OnReady SyncProperty Event
Updating the GotHit() Method
Handling the OnChanged SyncProperty Event
Adding listeners to the "hp" SyncProperty
Make sure you applied the changes to the Player Prefab and disabled the Player GameObject in the Hierarchy.
Play
You can follow the steps in section "Test and Play" to test out the "hp" SyncProperty.
Last updated