Ethan Pelton
Ethan Pelton

Reputation: 1796

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)

Upvotes: 2

Views: 155

Answers (1)

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

You are hooking it to a function that doesnt exist,

"OnPlayName"

Should be

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

Upvotes: 2

Related Questions