- Home /
UNET Health isn't syncing
Hello,
I'm new to Unet and am struggling with syncing my health. For some reason, my health is only syncing and behaving correctly on the host. Here is a gif that shows my problems:
Anyways, here is my code for the Player and the Player UI.
Thanks a lot!
Answer by MarshallN · Feb 22, 2017 at 09:49 PM
I've just been helping out another user with this ; take a look at the comment chain on this question.
Long story short, [SyncVar(hook)] only calls that hook on clients. If you observe the player health in the inspector, you should see it changing, but since the bar is only updated when the hook is called, the visuals are off. You should use [Command] and [ClientRpc] to sync a function between clients (example here), or else just have the health bar check in Update() if it's different than the health value itself, and if so change.
Yeah but the thing is, I'm updating the ui with Update() inside the PlayerUI script, for now OnHealthChanged is only used to test and call Die()
Hmm... Looking at your code, you're only running Update if you're the local player, and if you are, then TakeDamage is run upon pressing $$anonymous$$. However, TakeDamage returns if you're not the host - meaning that any player who isn't the host can't take damage. I haven't worked with [ReadOnly] and can't find the reference for it, so I dunno if that's tripping you up, but that's what I see first.
ReadOnly is just a custom script that I got online to allow me to see a variable in the inspector without allowing to edit it (just so I don't edit the wrong variables)
That'd be why I can't find its reference, then, haha. So that's not the issue.
If changing the TakeDamage function so that it doesn't return if it's not the server's script doesn't work, try adding a script that damages all players if a button is pressed, and test that on the host to see if it syncs properly.
Hey, so I rewrote my code to hopefully make it work better. So now, the health is getting synced up, but when I try to modify the UI from OnHealthChange, I get an error "RPC Function RpcSetHealthValue called on client."
Also, my RpcSetHealthValue is just this:
[ClientRpc]
public void RpcSetHealthValue(int value)
{
// healthBar is a UI slider object
healthBar.value = value;
}
Ok, so I got it to work! What I did is made all the methods that modify the health Commands, and completely removed the OnChangeHealth method as I found a way to work without it.
I also changed my update to this so that the TakeDamage wouldn't be called on all players when doing it on the client.
private void Update()
{
if (isLocalPlayer)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$))
CmdAttack();
}
}
Nicely done, congrats on getting it to work!
Your answer
Follow this Question
Related Questions
SyncVar issue 0 Answers
Unity networking tutorial? 6 Answers
Synchronizing parented objects' position and rotation & handling player collision 0 Answers
How to call [Command] on Client in UNet 1 Answer
Client can't spawn GameObject's 0 Answers