Spawning all players after loadlevel

Hello! Im new with networking and im making my first multiplayer game. Everything has been almost too easy to implement but i’ve been hitting my head to a wall for few days with this issue.

I want to make a chat window (similar with league of legends) where player selects his character before the game starts. Im having problems with spawning all player characters, the game works fine if the server is initialized and connected in the same scene so i can call spawnPlayer function inside OnConnectedToServer and OnServerIntitialized. I need however load a new scene after the chat window is over.

If i try to call spawnPlayer inside function Start or OnLevelWasLoaded the new players cant see the players that are already in the game.

in menu scene:

if (GUILayout.Button("New Server")){
	Network.InitializeServer(32,parseInt(ServerPort),false);
}

if (GUILayout.Button("Connect")){
     Network.Connect(Mynetwork.ServerIP,parseInt(Mynetwork.ServerPort));
}

function OnServerInitialized(){
	Application.LoadLevel("Testbed");
	
}

function OnConnectedToServer(){
	Application.LoadLevel("Testbed");
}


In game scene:

function OnLevelWasLoaded()
{
	spawnPlayer();
}


function spawnPlayer()
{

	var PlayerTransform : Transform = Network.Instantiate(player, Vector3(0,10,0) ,transform.rotation, 0);
	var playerName : String = Mynetwork.PlayerName;
	
	PlayerTransform.networkView.RPC("setPlayerProperties", RPCMode.AllBuffered, playerName);

}

If a new player connects he is getting error “Received state update for view id’ AllocatedID: 1’ but the NetworkView doesn’t exist” the host however works just fine. So my question is, where i should actually call this spawnplayer function?

What’s happening is when you connect to an already running game it loads all of the players that were created by the spawnPlayer() function, but then it loads the new level which destroys all the objects in the current scene (including the already existing players).

What you are doing is this:

  • Client Joins Game
  • Client loads all of the players
  • Client loads new scene, which destroys all players
  • Client spawns, but can’t see the other players because they are not there anymore

Here is the solution:

function OnConnectedToServer () {
        Network.isMessageQueueRunning = false;
        Application.LoadLevel("Testbed");
}

If you add the line Network.isMessageQueueRunning = false, it will disable the network messages until you turn it on again.

function OnLevelWasLoaded () {
        Network.isMessageQueueRunning = true;
        spawnPlayer();
}

That will do this:
-Client joins Game and stops the message queue, so it is still waiting to load the players
-Client loads new scene
-Client starts the message queue, so it can now load the players
-Client spawns

Hope this helps! :smile:

didn’t quite solve it. Bear in mind that “in game scene” script is in seperate script in testbed scene.

Hmm… I tested your code with my changes and it seemed to work. Having those two separate scripts shouldn’t make a difference.

Well, im not getting the error anymore, but the newly joined player cannot see the hosts character.

But you’re right, i can see that the player character is spawned in the menu scene if i delete the loadlevel line :smile:

EDIT: Now it works! Thank you for the help :slight_smile: