Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party PUN2 free - OnJoinedRoom() not being called

Discussion in 'Multiplayer' started by Phoenix248, May 21, 2021.

  1. Phoenix248

    Phoenix248

    Joined:
    Sep 20, 2019
    Posts:
    52
    Hello everyone!

    I'm helping a friend with online setup for a game using PUN2 free, and for some reason, the override void OnJoinedRoom is not being called.

    The weird is that the exact same code and scene works fine on my computer, but not in his.

    I debbuged the client state and enabled the Support Logger. The logs are the following:
    Erro 0.PNG
    Erro 1.PNG

    In mine, Support Logger shows OnCreatedRoom AND OnJoinedRoom, but not in his.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using UnityEngine.UI;
    6. using Photon.Realtime;
    7.  
    8. public class ConnectionManager : MonoBehaviourPunCallbacks
    9. {
    10.     [SerializeField]private Text connectionMsg;
    11.  
    12.     [SerializeField] private GameObject lobbyPanel;
    13.     [SerializeField] private GameManager gameManager;
    14.     public InputField createRoom;
    15.     public InputField joinRoom;
    16.  
    17.     void Awake()
    18.     {
    19.         PhotonNetwork.GameVersion = "0.1";
    20.  
    21.         PhotonNetwork.ConnectUsingSettings();
    22.  
    23.     }
    24.     private void Start()
    25.     {
    26.         PhotonNetwork.AutomaticallySyncScene = false;
    27.         DontDestroyOnLoad(this.gameObject);
    28.  
    29.     }
    30.  
    31.     public override void OnConnectedToMaster()
    32.     {
    33.         base.OnConnectedToMaster();
    34.         PhotonNetwork.JoinLobby();
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         Debug.Log(PhotonNetwork.NetworkClientState.ToString());
    40.     }
    41.  
    42.     public void OnClickCreateRoom()
    43.     {
    44.         if(!PhotonNetwork.IsConnected)
    45.         {
    46.             connectionMsg.text = "Photon Network isn't connected";
    47.             return;
    48.         }
    49.  
    50.         if (createRoom.text.Length > 3)
    51.         {
    52.             PhotonNetwork.JoinOrCreateRoom(createRoom.text, new RoomOptions{MaxPlayers = 5 }, default);
    53.         }
    54.     }
    55.  
    56.     public override void OnJoinedRoom()
    57.     {
    58.         base.OnJoinedRoom();
    59.         connectionMsg.text = "Entered room sucefully";
    60.  
    61.         gameManager.JoinedRoom();
    62.     }
    And i think the reason can be related to this weird symbol in PhotonServerSettings, don't know if that's common, but in mine it is different.

    PhotonServerSettings.PNG


    if anyone can help, i would be grateful!
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    There is a Nullreference Exception in the second log. It gets caught somewhere but code that follows it, will not be executed.
    Look at the callstack and figure out what is null, then why it is not set.
    Typical issues are related to the order of Awake, OnEnable and Start. All of the Awake calls (e.g.) are done in the same situation but the order in which objects Awake can change. So maybe some script refers to some object that did not yet Awake or something such.
     
    Phoenix248 likes this.
  3. Phoenix248

    Phoenix248

    Joined:
    Sep 20, 2019
    Posts:
    52
    Thanks for the answer!

    I don't think this exception is related to awake order, cause it happens when i create a room, at this moment, the game is already running for a while, and no gameObjects are deleted or instantiated, so o think everything is set.

    And the void OnCreatedRoom() that the exception refers, is called normally, despite the null reference:

    Code (CSharp):
    1.  public override void OnCreatedRoom()
    2.     {
    3.         base.OnCreatedRoom();
    4.         connectionMsg.text = "Room created sucefully";
    5.     }
    when a room is created, my text component is changed correctly to "Room created sucefully". The problem is, the OnJoinedRoom() that should be called next, is not being called. And weirdly, the networkClientState that i debbuged in update, logs "Joined"...
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Then you have to double check that your script is registered to get the callbacks.
    If you inherit MonoBehaviourPunCallbacks, overrides of OnEnable and OnDisable need to call the base to be registered. The callback implementations are empty.
     
  5. Phoenix248

    Phoenix248

    Joined:
    Sep 20, 2019
    Posts:
    52
    But as i said, this code works perfectly in my project. But the problems above occurs with the same code in my friend's computer.

    Even reimporting PUN2 and reinstalling Unity didn't solve. Is there some setup in unity that controls callbacks and could be default missconfigured? Or a config in photon that could be messing the call of OnJoinedRoom()?

    I've tried everything at this point, no ideia why it works on my project and not in his.
     
  6. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    This is very odd indeed.
    The only thing that comes to my mind are race conditions when scripts all do something in Awake (etc) and the order in which the individual Awake methods gets called is different on some machine.
    The callback OnJoinedRoom() is simply a call to a component via implemented interface. You can see all of that in the LoadBalancingClient (and related) code. You could even log all registered scripts and or any change (register / unregister). Then you know which scripts are registered.
    Did you run this with the SupportLogger being enabled? If you do, grab the resulting logs from 2 different machines and compare that the SupportLogger tells you.