LoadLevel RPC not working

My RPC function is not working i already tried to do a lot of combinations to make it work
anyway the RPC its supposed to make a LoadLevel on a client, but it seems like only the local player gets the message, but even it dont load.
Also the client player (the one who should receive the RPC) kinda load the level, but simply stop at half load, and do not even print the messages i left inside the loadlevel code
in resume it simply get stuck, dont know why, i already tried with PhotonNetwork.LoadLevel(string level); but still the same.

This is the pieces of code i used:


[code2=csharp]public void LoadNet(PhotonPlayer player,string level){//My last try to make it work
photonView.RPC("LoadLevel", player, level);
}

[RPC]
public void LoadLevel(string level){//The RPC message receiver
Debug.Log("Read1!");
StartCoroutine(Load(level));
}

IEnumerator Load(string level){//The real function


PhotonNetwork.isMessageQueueRunning = false;
PhotonNetwork.SetSendingEnabled(0, false);
Debug.Log("Read2!");

Debug.Log("Loading level");
Application.LoadLevel(level);

Debug.Log("Starting load");
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();

Debug.Log("Load finished");//This messages only the client player has printed them still asking myself why
PhotonNetwork.isMessageQueueRunning = true;
PhotonNetwork.SetSendingEnabled(0, true);

}[/code2]

[code2=csharp]void OnPhotonPlayerConnected(PhotonPlayer player){//At player connect

if(PhotonNetwork.isMasterClient){

//OSC is a variable containing the other script
OSC.LoadNet(player, ThisMapName);//This was my last try
//In a older version (i think 1.9 or something) of photon it worked, now in 3.0 its not

}

}[/code2]

Comments

  • When calling Application.LoadLevel you are asking to destroy your current scene's gameobjects and load a new scene.

    With Unity, objects are destroyed at the end of a frame.

    LoadLevel is an asynchronous method: your level is not yet loaded when it calls yield return new WaitForEndOfFrame();.
    When the frame is ended, your scene is destroyed, so does your script. That is why it interrupts after yield return new WaitForEndOfFrame(); and don't go further.

    To avoid this you have to specify this: DontDestroyOnLoad(gameObject); which make the gameobject persistent through level changes.

    Rather than using yield return new WaitForEndOfFrame(); you could use: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html?from=Application

    Another option would be to use Application.LoadLevelAsync and check for the isDone value to be true.
  • Forgot to say, yes i used DontDestroyOnLoad()
    And the objects still there (Only the onlineSystem i made and the Chatmanager as i coded in the script)
    But anyway it wont load the scene, as you said the objects will be destroyed and that is not a problem, every object i need still on the scene, but the scene wont load, i checked the hierarchy and the objects still there, but the GUI disappear (that means i entered the room successfully) and the masterClient its supposed to send an RPC to the connecting client with the name of the Map (which its not working but anyway a level loads somehow and get stuck)
    This code used to work in Unity 3.5.6 dont know why in 3.5.7 wont work.
  • I made a lot of tests and none of my RPCs are being sended or received..
    Whats wrong??? :(
  • First of all, you can perfectly call a RPC that is a IENumerator, so there's no need for the extra void LoadLevel method.

    The following part looks buggy:
    Application.LoadLevel(level);
    yield return new WaitForEndOfFrame();
    Debug.Log("Load finished");//This messages only the client player has printed them still asking myself why
    PhotonNetwork.isMessageQueueRunning = true;
    PhotonNetwork.SetSendingEnabled(0, true);

    }

    I wouldthink everything after Application.LoadLevel() NEVER executes. So that means that the photon network queue is never turned on again.

    The solution to your struggles is to simply use the new PUN features to handle network loading for you.

    1) You can have PUN auto synch scenes.
    PhotonNetwork.automaticallySyncScene = true;
    Run this once in your preloader or mainmenu to ensure this is set true on all your clients. Now whenever the masterclient switches its level, all other clients will switch as well.

    2) To load a level (on the masterclient or anyone else..) simply use:
    PhotonNetwork.LoadLevel( string levelName);
    This will take care of disabling and enabling the network queue temporary. Combined with 1 this will make sure everyone loads the correct level.
  • Leepo wrote:
    First of all, you can perfectly call a RPC that is a IENumerator, so there's no need for the extra void LoadLevel method.

    The following part looks buggy:
    Application.LoadLevel(level);
    yield return new WaitForEndOfFrame();
    Debug.Log("Load finished");//This messages only the client player has printed them still asking myself why
    PhotonNetwork.isMessageQueueRunning = true;
    PhotonNetwork.SetSendingEnabled(0, true);

    }

    I wouldthink everything after Application.LoadLevel() NEVER executes. So that means that the photon network queue is never turned on again.

    The solution to your struggles is to simply use the new PUN features to handle network loading for you.

    1) You can have PUN auto synch scenes.
    PhotonNetwork.automaticallySyncScene = true;
    Run this once in your preloader or mainmenu to ensure this is set true on all your clients. Now whenever the masterclient switches its level, all other clients will switch as well.

    2) To load a level (on the masterclient or anyone else..) simply use:
    PhotonNetwork.LoadLevel( string levelName);
    This will take care of disabling and enabling the network queue temporary. Combined with 1 this will make sure everyone loads the correct level.


    Still the same thing, i tried already with PhotonNetwork.LoadLevel();
    I simply got frustrated and got back to unity 3.5.6
    my photon on that version works better dont know why