- Home /
Multiplayer Name Labels problem
Hi Unity folks, It's been a while since my last question and I need your sweet help for a multiplayer issue that I've encountered two days ago.
I'm setting up an Online World War 2 battle game where you can control an airplane and you can (eventually) customize it as you like.
Random User: "Hey man, that sounds like a game copy!" Well, ye...NO!
Ahah, just kidding!
I'm trying to display the player name and aircraft name above a player, with no luck (otherwise I wouldn't have come here, right?)
You can already control your aircraft and fly around, but it's really hard to see other players since the map it's quite big ;) . I've attempted to store the username that a client entered in a GUI text field and then send it to a spawner script (with a networkView attached to it) with an RPC call. It pops an error saing >> "No RPC can be sent since no connection was started" << I thought that it can't send that RPC because it's still connecting to the server and so...nothing happens.
Then in the spawner script I've changed so that when it is already connected it sends to the spawned player the RPC to set the name but then nothing happens because I can't send the name typed in by the user in the client to the spawner.
I was thinking about storing all the players and the corresponding aircraft name into a List ordered by player's id in the server and then send those names to all the players connected, but...I don't really know how to do it.
By the way I'll put down the Client, Server and the Player name script to see if you guys can help me out.
I'm not asking to do an entire script for me, I'm asking if someone can enlight me on this subject (network names) and eventually put some examples on how to do an array storing all players and send those stored values to all the others.
======================================= Client script
var yourName : String = "";
var SpawnPointObjectBlue : GameObject;
@HideInInspector
var currentMenu : String;
var MenuSize : Vector2;
@HideInInspector
var data : HostData[];
function Awake()
{
MasterServer.RequestHostList("Warbirds PvP");
currentMenu = "UserSettings";
}
function OnGUI() {
if(currentMenu == "ServerList")
GUI.Window(0,Rect((Screen.width/2)-(MenuSize.x/2),(Screen.height/2)-(MenuSize.y/2),MenuSize.x,MenuSize.y),Client_Window,"Server List");
else if(currentMenu == "UserSettings")
GUI.Window(1,Rect((Screen.width/2)-(MenuSize.x/2),(Screen.height/2)-(MenuSize.y/2),MenuSize.x,MenuSize.y),User_Window,"User Settings");
}
function Client_Window (windowID : int)
{
if(GUILayout.Button("Go to User Settings"))
currentMenu = "UserSettings";
GUILayout.BeginHorizontal();
for (var element in data)
{
var connections : int = parseInt(element.connectedPlayers)-1;
var connectionsLimit : int = parseInt(element.playerLimit)-1;
var serverConnections = connections.ToString() + " / " + connectionsLimit.ToString();
GUILayout.Box(element.gameName);
GUILayout.Box(serverConnections);
GUILayout.Box(element.comment);
if (GUILayout.Button("Connect"))
{
Network.Connect(element);
currentMenu = "";
//GameObject.FindWithTag("Respawn").networkView.RPC("SpawnPlayer",RPCMode.AllBuffered, yourName);
}
}
GUILayout.EndHorizontal();
}
function User_Window (windowID : int)
{
yourName = GUILayout.TextField(yourName); //This is where I store the player name
if(GUILayout.Button("Go to Server List"))
{
currentMenu = "ServerList";
data = MasterServer.PollHostList();
}
}
======================================= Spawner script
var Player : GameObject;
var myName : String;
function OnConnectedToServer ()
{
if(Network.isClient)
{
if(Player)
{
var InstPlane : GameObject = Network.Instantiate(Player, transform.position, transform.rotation, 0);
InstPlane.networkView.RPC("SetMyNames",RPCMode.AllBuffered, myName, "P39D Airacobra");
}
}
}
function OnPlayerDisconnected (player : NetworkPlayer)
{
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
@script AddComponentMenu("Network/Spawner Script");
======================================= NameLabel script
var AirplaneName : String = "";
var NetworkPlayers : GameObject[];
var screenPos : Vector2;
var MyCam : Camera;
var myOffset : float = 1.0;
function Update ()
{
NetworkPlayers = GameObject.FindGameObjectsWithTag("Player");
for(var p : GameObject in NetworkPlayers)
{
screenPos = MyCam.WorldToScreenPoint(p.transform.position);
}
}
function OnGUI ()
{
for(var p : GameObject in NetworkPlayers)
{
if(p.networkView.isMine == false)
{
GUI.Label(new Rect(screenPos.x-100, Screen.height - screenPos.y-myOffset, 200, 50), p.GetComponent(MultiplayerNameLabel).PlayerName+"/n"+"["+p.GetComponent(MultiplayerNameLabel).AirplaneName+"]");
}
}
}
@RPC
function SetMyNames (p_name : String, a_name : String)
{
PlayerName = p_name;
airplaneName = a_name;
}
I hope you guys can help me out cause I can't go on without names above players (well you know better than me!)
Thanks in advance
See you soon
Bombix95
Your answer
![](https://koobas.hobune.stream/wayback/20220613161343im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simple Multiplayer Animation Question 1 Answer
Network RPC buffer issue 0 Answers
How to use Network.Instantiate Groups 1 Answer
Displaying players name above in multiplayer 1 Answer
How I can convert data to Wifi/ Multiplayer/ Network 0 Answers