Search code examples
c#unity-game-engineunity3d-unet

unable to set text with syncvars in unity


I'm ultimately trying to show active players, but I'm having trouble setting the value of a text box.

My understanding is that once a player joins, I need to use syncvars to show all player names on each client. So to start, I'm trying this...

[SyncVar(hook = "OnPlayerName")]
public string name;

void OnPlayerName(string newPlayerName)
{
    playerlist.text = name;

}

public override void OnStartClient()
{
    name = "terrance";
}

When I run this, the text box is empty. If I set the text without syncvars, it works. Of course, the change is only visible to the local client if I do it without syncvars.

RESOLUTION: the parameter newPlayerName needs to match the variable name, so in this case "name"...

void OnPlayerName(string name)

Solution

  • You are hooking it to a function that doesnt exist,

    "OnPlayName"

    Should be

    [SyncVar(hook = "OnPlayerName")]
    public string name;