Update SyncVars.md

This commit is contained in:
MrGadget 2019-09-11 15:07:08 -04:00 committed by GitHub
parent 414831d210
commit 58be4d44fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,10 +20,10 @@ public class Enemy : NetworkBehaviour
[SyncVar]
public int health = 100;
private void OnMouseUp()
void OnMouseUp()
{
NetworkIdentity ni = NetworkClient.connection.playerController;
PlayerController pc = networkIdentity.GetComponent<PlayerController>();
PlayerController pc = ni.GetComponent<PlayerController>();
pc.currentTarget = gameObject;
}
}
@ -52,4 +52,4 @@ public class PlayerController : NetworkBehaviour
}
```
In this example, when a Player clicks on an Enemy, the networked enemy game object is assigned to `PlayerController.currentTarget`. When the player presses X, with a correct target selected, that target is passed through a Command, which runs on the server, to decrement the `Enemy.health` SyncVar. All clients will be updated with that new value. You can then have a UI on the enemy to show the current value.
In this example, when a Player clicks on an Enemy, the networked enemy game object is assigned to `PlayerController.currentTarget`. When the player presses X, with a correct target selected, that target is passed through a Command, which runs on the server, to decrement the `health` SyncVar. All clients will be updated with that new value. You can then have a UI on the enemy to show the current value.