Help with Network Script. Game with multiple Maps.

Hey guys.

So I try making a script which makes it so that when a new player connects to a server he automatically loads the map currently played on the server.

Here is my script:

private var Map : String;
private var lastLevelPrefix = 0;

function Awake ()
    // Network level loading is done in a separate channel.
    DontDestroyOnLoad(this); = 1;

function OnConnectedToServer() {
var viewID : NetworkViewID= networkView.viewID;
networkView.RPC("CallServerSync", RPCMode.Server, viewID);

//Here was the part which makes it so that the server loads the selected map. I deleted it for this question

function LoadLevel (level : String, levelPrefix : int)
	lastLevelPrefix = levelPrefix;

		// There is no reason to send any more data over the network on the default channel,
		// because we are about to load the level, thus all those objects will get deleted anyway
		Network.SetSendingEnabled(0, false);	

		// We need to stop receiving because first the level must be loaded first.
		// Once the level is loaded, rpc's and other state update attached to objects in the level are allowed to fire
		Network.isMessageQueueRunning = false;

		// All network views loaded from a level will get a prefix into their NetworkViewID.
		// This will prevent old updates from clients leaking into a newly created scene.

		// Allow receiving data again
		Network.isMessageQueueRunning = true;
		// Now the level has been loaded and we can start sending out data to clients
		Network.SetSendingEnabled(0, true);

function CallServerSync(viewID : NetworkViewID){
networkView.RPC("GetServerInfo", viewID.owner,Map);

function GetServerInfo(ServerMap:String){
Debug.Log("Server got called!");
networkView.RPC( "LoadLevel", RPCMode.AllBuffered, Map, lastLevelPrefix + 1);

@script RequireComponent(NetworkView)

The script is attached to the Main Camera which handles the menu.

The part of the server works. CallServer Sync gets called but GetServerInfo doesn’t.
I don’t get any errors on the client. The Client just get stuck on the “Starting the game”-Screen.

I really hope someone can help me.


I changed the CallServerSync function like Mike said but that didn’t solve my problem. Nothing really changed. GetServerInfo still doesn’t get called on the client.


I currently went back to another method where I pass the information through the comment because this way doesn’t seem to work like I wanted it to. If some of you still have an idea of how I could get this method working I would be happy to hear it.

Your call Server Sync function looks a bit strange - I think it should look like this:

function CallServerSync(viewID : NetworkViewID){
     networkView.RPC("GetServerInfo", RPCMode.All,Map);

Also - you are aware that networkView.RPC("CallServerSync", RPCMode.Server, viewID); will not happen if you are running that code on the server.

So I’m gonna reactivate this question because I tried some stuff as a result of the master server going down.

Now I tried a more simple version:

function OnConnectedToServer()
var ClientID = networkView.viewID;
transform.networkView.RPC("PlayerConnect", RPCMode.Server, ClientID);

function PlayerConnect(Client : NetworkViewID){
if(Map1) //is true if Map 1 is played
networkView.RPC("LoadLevel1", Client.owner);
if(Map2) //is true if Map 2 is played
networkView.RPC("LoadLevel2", Client.owner);

function LoadLevel1()

function LoadLevel2()

function OnNetworkLoadedLevel(){

This is a part of a script attached to the MainCamera which has a NetworkView. Also in the Awake function I put in DontDestroyOnLoad(this).

That doesn’t really work unfortunately. As a server I get error messages that the RPC wasn’t called because PlayerConnect doesn’t exist in a random object in the scene. The object stays the same each time I try but it’s always a different one when I change Map/Scene.

The Client can’t load the map as a result of that.

I hope someone find the problem in my code. I really do as I get this kind of error everytime I try to pass the map through RPC’s.

Every push in the right direction is much appreciated.