0

I'm planning to send a integer value to CallUpdatePlayerName after SetPlayerName is called is this possible? Below is my code

[SyncVar(hook = nameof(CallUpdatePlayerName))]
public string playerName;

[Command]
public void SetPlayerName(string value)
{
    if (isLocalPlayer)
    {
        playerName = value;
    }
}

private void CallUpdatePlayerName(string oldPlayerName, string newPlayerName, int index)
{

}
2
  • 1
    is your integer in the same script? or where you planing to get it?
    – joreldraw
    Commented Jul 1, 2021 at 8:51
  • Yes .. but not via the hook ...
    – derHugo
    Commented Jul 2, 2021 at 23:32

1 Answer 1

1

No you can't, not like this.

In such case don't use a SyncVar at all but rather something like e.g.

public void SetPlayerName(string name)
{
    if(!isLocalPlayer) return;
    
    playerName = name;

    // Tell the host
    CmdSetPlayerName(name, yourIndex);
}

[Command]
private void CmdSetPlayerName (string name, int index)
{
    playerName = name;

    // Forward to all other clients
    RpcSetPlayerName(name, index):
}

[ClientRpc]
private void RpcSetPlayerName (string name, int index)
{
    playerName = name;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.