Hey guys, my issue is pretty weird… Maybe I just don’t wanna understand Netcode but here is the code:
using Unity.Netcode;
using UnityEngine;
public class Combat : NetworkBehaviour
{
public float health;
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SendDamageServerRpc(10f);
}
if (Input.GetKeyDown(KeyCode.N))
{
health = 1f;
}
}
[ServerRpc]
void SendDamageServerRpc(float receivedDamage)
{
ReceiveDamageClientRpc(receivedDamage);
}
[ClientRpc]
void ReceiveDamageClientRpc(float applyDamage)
{
health -= applyDamage;
if (health <= 0)
{
Application.Quit();
}
}
}
Heres my issue: It generally works. After pressing “R” ten times, all clients and the hosts game quits.
But it appears the variable health on everyones client never physically changes. The reason I say this, is because if i press N on any client, his local health (should) get set to 1 so he would be onehit. So now if anyone presses R once, that onehit clients health drops below zero and his game would quit, right? Well no. He still requires ten R presses like everyone else for his game to quit. I just dont get it!
Thanks in advance!
EDIT: I fixed it. turns out i just didnt understand that client and server rpcs actually get executed by the same gameobject that invoked it but onserver/client side.