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

Calling networkclient from the previous scene (menu)

Discussion in 'Multiplayer' started by Royall, Jul 26, 2015.

  1. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    120
    So i got my login system working. I have a scene which acts as the main menu where the player does the login/character creation/character select.

    Now I have this to setup the NetworkClient:
    Code (CSharp):
    1. public class MenuController : MonoBehaviour {
    2.     public NetworkClient myClient = new NetworkClient();
    Then when connecting to the server I register handlers for custom messaging:
    Code (CSharp):
    1. myClient.RegisterHandler(MainController.sendAvatarID, OnSendAvatar);
    Problem is when I login en go to the next room (world) everything works fine but I can't get custom messaging to work cause myClient only exists in MenuController. For the world scene I have a client controller:
    Code (CSharp):
    1. public class ClientController : MonoBehaviour {
    2.     void Start () {
    3.         mainController = GameObject.Find("MainController").GetComponent<MainController>();
    4.         myClient.RegisterHandler(MainController.updateInventoryID, OnUpdateInventory);
    5.     }
    6.    
    7.     void OnUpdateInventory(NetworkMessage mess) {
    8.         var msg = mess.ReadMessage<MainController.updateInventory>();
    9.         Debug.Log("Inventory "+msg.inventory);
    10.     }
    11. }
    This will ofcourse not work cause myClient doesnt exist anymore as it was in the previous scene. Already tried to replace it with just NetworkClient but no luck.

    I can't register the message handler in the menu script because then OnUpdateInventory wont be called on the ClientController script :(

    Anyone has a solution?