Hey all,
I am hoping to get some help from anyone familiar with FishNet, the multiplayer networking solution.
I have created a basic script for decreasing a players health when the “R” key is pressed. FishNet recently updated to v4, and so SyncVars work a little different than before. I’ve clearly done something wrong as my clients health decrease as expected when I run builds locally, with a client on my localhost acting as both server + client, but when I do a build, and upload it to a hosting service such as Edgeware, the health doesnt decrease for either player.
Its a very basic script so Ill paste it below. Any help is appreciated. I have other scripts that do some basic things using Server and Observer RPCs. These work as expected, however I cant seem to figure out the sync vars.
using FishNet;
using UnityEngine;
using FishNet.Object;
using FishNet.Object.Synchronizing;
using UnityEngine.UI;
public class PlayerHealth : NetworkBehaviour
{
public readonly SyncVar<int> health = new();
Text healthText;
public override void OnStartClient()
{
base.OnStartClient();
if (!base.IsOwner)
GetComponent<PlayerHealth>().enabled = false;
}
private void Awake()
{
gameObject.name = "Player:" + InstanceFinder.NetworkManager.ClientManager.Clients.Count;
health.Value = 100;
healthText = GetComponentInChildren<Text>();
health.OnChange += OnHealthChanged;
}
private void OnHealthChanged(int oldValue, int newValue, bool asServer)
{
healthText.text = "H: " + newValue;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
UpdateHealth(this, -1);
}
}
[ServerRpc]
public void UpdateHealth(PlayerHealth script, int amountToChange)
{
UpdateHealthText(script, amountToChange);
}
[ObserversRpc]
public void UpdateHealthText(PlayerHealth script, int amountToChange)
{
script.health.Value += amountToChange;
}
}