Hello.
I got a simple multiplayer game working but somehow the health bar isn’t updating properly as soon as a second player joins. The strange thing is, the values in the sliders are correct at both the server and the client, even the fill image is the right size, but the bar still only shows the white background.
Anyone has any idea?
You probably need to just update the bars again with the current values of the health bars. Put your health bar code in OnStartClient() or maybe in Start(), forgot which one since you have to know when Mirror does the synching for the SyncVars.
You probably need to just update the bars again with the current values of the health bars. Put your health bar code in OnStartClient() or maybe in Start(), forgot which one since you have to know when Mirror does the synching for the SyncVars.
Hmm, in case you mean that on the client i just have to set the value of the slider component after the second player joined i don’t think that helps. Because as i said when damage is done the value on the client and server update correctly in the inspector. in case there is a function in the unity API for the slider component that updates the graphical portion, i will try it and thanks for the suggestion.
I looked more into the problem and it seems there is something very strange (to me) going on with the cameras and the UI.
I’m not talking about the value on the client. Sync Vars are automatically updated and passed to the clients from the server. When a new client joins, they should receive the most updated value, but it doesn’t mean that you have updated the health bar UI that is connected to that health value . That’s why you should explicitly rerun your code/logic that adjusts the UI based on the current health. Do you have sync var hooks for your health value?
Yes, the value for health is a syncvar, and it is correct on server and client as well. in my other posts i was talking about the value in the slider. The float that controls the fill part in the slider object, And that value is getting updated correctly too. It is really only the visual part in the camera, that somehow is wrong, and it has something to do with the cameras.
I found out that the player prefab doesn’t spawn correctly on the client. it is missing some childs of the camera, wich is also a child of the player prefab.
Right now i can’t really look into it. Maybe this evening (EU) i can get back to it.
Not sync var, but sync var hooks. Sync var hooks are methods that you can add to sync vars that will be called on the client when the value of the sync var is changed. For example, your sync var hook can be a method or something that changes the UI.
[SyncVar(hook=nameof(OnHealthChange)]
int healthValue = 100
private void OnHealthChange(int oldValue, int newValue)
{
// put your code that changes the Health UI here that shows your hp increasing or decreasing
}
For UNet, syncvar hooks do not automatically run when you have a late client, but I don’t remember if Mirror changed this.
Check the documentation here: SyncVar Hooks | Mirror