- Home /
Is it possible to find which client/server pressed a GUI.Button?
I'm working on an online multiplayer game and I've ran into a problem, I'm adapting a script which I have found in a different project to work with my game, and I think I have found the problem. In the original script players were instantiated either by OnServerInitiated, for the server, and OnPlayerConnected for the clients, However, this does not work for my game because I players connect to the server in the menu (before the scene with the script starts) therefore these functions are never activated since what would activate them has happened in the previous scene. I fixed this problem for the server by adding a GUI.Button that instantiates a player when it is pressed, and finds the server using Network.player, however, I cannot find the client who connects because the only way I know how to do this is using OnPlayerConnected(newplayer : NetworkPlayer). Is it possible to find the player by detecing who pressed the button?
Heres my code:
#pragma strict
#pragma implicit
#pragma downcast
public var playerPrefab : Transform;
public var playerScripts : ArrayList = new ArrayList();
var menuWidth : int = 100;
function OnGUI(){
GUILayout.BeginArea(Rect(Screen.width/2 - menuWidth/2, Screen.width/2 - 400, menuWidth, 400));
if(Network.peerType == NetworkPeerType.Server){
if(GUILayout.Button("Play", GUILayout.Height(20))){
Spawnplayer(Network.player);
}
}
if(Network.peerType == NetworkPeerType.Client){
if(GUILayout.Button("Join", GUILayout.Height(20))){
newPlayer = Network.player;
Spawnplayer(newPlayer);
}
}
GUILayout.EndArea();
}
function Spawnplayer(newPlayer : NetworkPlayer){
//Called on the server only
var playerNumber : int = parseInt(newPlayer+"");
//Instantiate a new object for this player, remember; the server is therefore the owner.
var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, playerNumber);
//Get the networkview of this new transform
var newObjectsNetworkview : NetworkView = myNewTrans.networkView;
//Call an RPC on this new networkview, set the player who controls this player
newObjectsNetworkview.RPC("SetPlayer", RPCMode.AllBuffered, newPlayer);//Set it on the owner
}
function OnNetworkLoadedLevel (){
Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613083214im_/https://answers.unity.com/themes/thub/images/avi.jpg)