I have this network script that works really well except for when I want to spawn a player at a different location. Im basing location on the number of current players connect. Right now the variable is updating right on the server so that good. But when a client joins the game, the player count for him is 0. I want to set that to the current number of players connected from the server variable. I was messing around with RPC functions but with no luck.
So this is what I have so far.
var playerCount : int = 0;
//player and jump master spawn componets
var playerPrefab : GameObject;
//spawn points
var sp1: Transform;
var sp2: Transform;
var sp3: Transform;
var sp4: Transform;
var sp : Transform;
function OnServerInitialized()
{
Debug.Log("Server initilized");
playerCount++;
spawnPlayer(sp1);
}
function OnPlayerConnected(networkPlayer:NetworkPlayer):void
{
Debug.Log (networkPlayer.guid + " connected");
playerCount++;
//spawnPlayer(sp2);
//spawning player here does not work! Just a black screen no cam perhaps
}
function OnConnectedToServer()
{
//loop though every object in the game
for(var go : GameObject in FindObjectsOfType(GameObject))
{
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
Debug.Log("Connected to server");
Debug.Log("Player Count: " + playerCount);
if(playerCount ==0)
{
sp = sp1;
}
if(playerCount ==1)
{
sp = sp2;
}
if(playerCount ==2)
{
sp = sp3;
}
if(playerCount ==4)
{
sp = sp4;
}
spawnPlayer(sp);
}
//spawn player prefab
function spawnPlayer(SP : Transform)
{
Network.Instantiate(playerPrefab, SP.position, Quaternion.identity, 0);
}
@RPC
function SendPlayerCount(player : NetworkPlayer )
{
//send playercount to client from server
networkView.RPC("assignvariableRPC",player,playerCount);
}
@RPC
function RecivePlayerCount(count : int)
{
//get playercount from server
//playerCount = playerCount;
playerCount = count;
}