I made a script for my multiplayer game to put name tags on their heads. You put your name in and it saves it to player prefs. Then when you connect it gets it from player prefs and is supposed to RPC to other players, but your name appears on everyones head. I can see why this doesnt work, but dont know a solution.
public class namePlates : MonoBehaviour {
public string getPlayerName;
public TextMesh NAMEPLATE;
//rotate to screen.
void OnPlayerConnected (NetworkPlayer player) {
NAMEPLATE = gameObject.GetComponent<TextMesh>();
networkView.RPC("getName", RPCMode.All);
}
// Update is called once per frame
void Update () {
}
[RPC]
void getName() {
getPlayerName = PlayerPrefs.GetString("Player_Name");
NAMEPLATE.text = getPlayerName;
}
}
how can i make it so that my name goes to other players.
As mentioned above the other players will as it is now get their locally saved name on every player.
So firstly if you want to send your name to other clients use:
networkView.RPC("getName", RPCMode.Others, PlayerPrefs.GetString("Player_name"));
Now the problem is that every player will get this name instead, and as i understand it you want the correct remote player to have the correct name over his/her head. So the next thing you need to do is to somehow sort out which remote player should have which name over the head.
Depending on how you handle the spawning of the remote players this can be done in multiple ways. I usually make a list of all connected players and assign them with indexes. So player 1 who joined have index 1 player two has 2 etc… This way you can then assign your index to your remote version on all clients.
If this is done you can do something like:
networkView.RPC("getName", RPCMode.Others, PlayerPrefs.GetString("Player_name"), index);
And on the recieving end:
[RPC]
void getName(string name, int index) {
remotePlayers[index].HeadText = name;
}
If the whole index thing was hard to understand/or you are using network.instantiate and not “remote” versions of the player, you can send the networkplayer id (or something similar) and compare that instead.
Hopefully this helped you somewhat, if you have questions regarding giving players index etc feel free to ask.