Search Unity

Third Party PhotonNetwork failed to instantiate prefab

Discussion in 'Multiplayer' started by m0rgul, Jul 14, 2015.

  1. m0rgul

    m0rgul

    Joined:
    Mar 17, 2015
    Posts:
    7
    I am getting this weird error when trying to instantiate a prefab over the Photon Network.

    Failed to Instantiate prefab: DefaultMale. Client should be in a room. Current connectionStateDetailed: Joined

    Basically what I do is connect to the photon network in the previous scene, join the room and then when the ser hits next, I load the next level where I should instantiate the prefab based on the user's options. But I get this error even tho I am in a room...

    Failed to Instantiate prefab: DefaultMale. Client should be in a room. Current connectionStateDetailed: Joined
    UnityEngine.Debug:LogError(Object)
    PhotonNetwork:Instantiate(String, Vector3, Quaternion, Int32, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2094)
    PhotonNetwork:Instantiate(String, Vector3, Quaternion, Int32) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonNetwork.cs:2077)
    Spawn:Spawnplayer() (at Assets/Scripts/Spawn.cs:71)
    Spawn:Start() (at Assets/Scripts/Spawn.cs:27)


    Anyone have any idea why this is?

    PS: I COULD connect and instantiate in the next scene (which is how it was until now...), but the problem is that it took some time for this to happen and the user had to just stand there for a few seconds while the connection is established. So I wanted to connect before the scene is actually launched in order to speed up the process a bit.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    That's interesting. The state is "Joined" but you are not allowed to instantiate?
    I guess the underlying networkingPeer.State is not connected. Could you add networkingPeer.State to that line's debug log, so we can see what State is?
    Which version of PUN do you use?

    Do you use PhotonNetwork.LoadLevel() with PhotonNetwork.automaticallySyncScene set to true?
     
    Mahnoor47 likes this.
  3. m0rgul

    m0rgul

    Joined:
    Mar 17, 2015
    Posts:
    7
    I tried with Photonnetwork.LoadLevel() and the simple LoadLevel (and LoadLevelAsync).No luck anyway I do it, so...if anyone has any other ideas, they are welcome.

    I do not use autoSyncScene, this is somewhat of an MMO, therefore anyone can join anytime they want to, no need to make everyone reload the scene for every player, right?
     
    Last edited: Jul 15, 2015
  4. m0rgul

    m0rgul

    Joined:
    Mar 17, 2015
    Posts:
    7
    ok, further updates, i did some debugging and came across this
    Code (CSharp):
    1. public static GameObject Instantiate(string prefabName, Vector3 position, Quaternion rotation, int group, object[] data)
    2.     {
    3.         Debug.Log ("connected:"+ connected+" InstantiateInRoomOnly: "+InstantiateInRoomOnly+", inRoom:"+inRoom);
    4.         if (!connected || (InstantiateInRoomOnly && !inRoom))
    5.         {
    6.             Debug.LogError("Failed to Instantiate prefab: " + prefabName + ". Client should be in a room. Current connectionStateDetailed: " + PhotonNetwork.connectionStateDetailed + ", Networking Peer State: "+networkingPeer.State);
    7.             return null;
    8.         }
    on one hand,
    Code (CSharp):
    1. PhotonNetwork.connectionStateDetailed
    tells me i have joined, on the other hand
    Code (CSharp):
    1. PhotonNetwork.connected
    is false. looking into that (since it is true in the previous scene), i came across this line:

    Code (CSharp):
    1. return !networkingPeer.IsInitialConnect && networkingPeer.State != PeerState.PeerCreated && networkingPeer.State != PeerState.Disconnected && networkingPeer.State != PeerState.Disconnecting && networkingPeer.State != PeerState.ConnectingToNameServer;
    where
    Code (CSharp):
    1. networkingPeer.IsInitialConnect
    is TRUE.

    anyone have any idea why it changed to true from one scene to the next?
     
  5. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    Any player who already loaded the scene, won't reload because someone joins. Without autoSyncScene, PhotonNetwork.LoadLevel() is basically useless (please don't ask why it's off by default).

    PhotonNetwork.connected is true, if !networkingPeer.IsInitialConnect (and the rest).
    IsInitialConnect
    keeps track if PUN could connect to any server. This is useful to know if we run into connection issues. If we never connected at all, the device might be offline, etc. This is another class of errors than losing connection.

    So much for the background.
    IsInitialConnect should be false. It's set true in all cases where you "connect". Maybe you do that in Start() in some script you load with the scene?? That would re-set this state.

    I would use the following code to get a log for every time something sets IsInitialConnect to true and check the logs.
    Unity logs a callstack, so you can actually see which method where caused setting this value. Especially look out for this line when you load a scene...

    Code (CSharp):
    1.  
    2.     public bool isInitialConnect = false;
    3.     public bool IsInitialConnect
    4.     {
    5.      
    6.         get { return isInitialConnect; }
    7.         set
    8.         {
    9.             if (value) Debug.Log("Setting isInitialConnect to true.");
    10.             isInitialConnect = value;
    11.         }
    12.     }
    13.  
     
  6. m0rgul

    m0rgul

    Joined:
    Mar 17, 2015
    Posts:
    7
    thank you, tobiass! that was indeed the issue. there was an object instantiated with the scene that also began connecting to the photon and it was responsible for resetting the value of IsInitialConnect.
     
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    I will try to figure out a way to avoid the issue in the future.
    Glad you could find it!
     
  8. harp19

    harp19

    Joined:
    Jul 5, 2021
    Posts:
    1
    thanks