Search Unity

Instantiate Problem For New Players

Discussion in 'Multiplayer' started by JamesElvin, Jul 29, 2014.

  1. JamesElvin

    JamesElvin

    Joined:
    Sep 19, 2013
    Posts:
    33
    I have recently started connecting clients to servers before actually loading the scene, which I have not done before. The problem I currently have is that players connect to the Photon cloud server, load the scene and instantiate but when a player connects after them, the previous player cannot see them.

    These are the lines of code that I am using.

    First Script - Attached to a game-object in the main menu


    Code (CSharp):
    1. private void OnJoinedRoom()
    2.     {
    3.         Debug.Log("We have joined a room.");
    4.         Application.LoadLevel ("Loading Screen 1");
    5.     }
    6.  
    7.     private void OnCreatedRoom()
    8.     {
    9.         Debug.Log("We have created a room.");
    10.         Application.LoadLevel ("Loading Screen 1");
    11.     }
    Second Script - Attached to a game-object in the game scene


    Code (CSharp):
    1.     void Start ()
    2.     {
    3.         GameObject player = PhotonNetwork.Instantiate("playerA", new Vector3(Random.Range(-75, -90), 112, Random.Range(180, 192)), Quaternion.identity, 0);
    4.     }
    I'm sorry for being a completely stupid but I am clueless of how to get clients to instantiate without error (so previous can view new players etc) into the new scene.

    Any feedback will be appreciated and I apologies for any lack of information or 'brains'. Let me know if I need to explain my situation a bit clearer, thank you.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    The problem will be related to this:
    When you enter a room, you get all Instantiate commands and PUN will usually just execute them. At that moment, the GOs exist.
    When your client decides to load another scene, the GOs that got instantiated will be destroyed. Like everything else in the "old" scene.

    You will want to make sure to load the fitting scene before you execute all Instantiate calls.
    One way is to use Custom Room Properties tor store the level for the room. They can be read before you get Instantiate calls. And you can make your client wait to execute Instantiates by pausing the message queue (PhotonNetwork.isMessageQueueRunning).

    You can also set PhotonNetwork.automaticallySyncScene = true and then use PhotonNetwork.LoadLevel(). It will use the approach I explained above and should just work.
     
  3. JamesElvin

    JamesElvin

    Joined:
    Sep 19, 2013
    Posts:
    33
    Thank you for the reply but the game objects are not being instantiated until the game scene has been loaded. Joining the room just loads the game scene.

    I have created some sort of fix, which I am not happy with but works (Each player has to be in the room and click play to spawn in. Players joining mid-game won't spawn though after the initial start).
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    I don't see where you disable the message queue but I do see that you LoadLevel when your client enters the room.

    When you join a room, the client gets all properties, all players, all Instantiates and buffered RPCs.
    The very same loop that calls OnJoinedRoom() will also execute all RPCs and Instantiates unless you pause execution for loading.
    I'd guess Unity will finish executing the frame (and in it all Instantiate and all buffered RPCs) and then loads the next scene.
    To verify it's an issue with loading, just comment out those load lines. You are very likely seeing all GOs you expect.