Search Unity

UNET LAN match no player game objects in game scene on second game.

Discussion in 'UNet' started by BeforeTheLight, Oct 14, 2018.

  1. BeforeTheLight

    BeforeTheLight

    Joined:
    Jan 7, 2011
    Posts:
    168
    I am working on a multiplayer game where you play quick 2 minute games and return to the lobby select new equipment and then play again. Seemed simple enough but when I start the second game my game objects for the players are not in the game scene. I say it like that because I can see where OnLobbyServerSceneLoadedForPlayer gets called and then Start gets called on the player objects, then the player game objects get destroyed. My guess is for some reason the player objects are getting spawned in the lobby then it gets destroyed but I cannot figure out why that could be happening. I am not overriding
    OnLobbyServerCreateLobbyPlayer, OnLobbyServerCreateGamePlayer, or OnServerAddPlayer.
    I am overloading OnLobbyServerSceneLoadedForPlayer and the code for that is below. Thanks for any assistance in advance.

    Code (CSharp):
    1.  
    2. //This just copies info from the lobby player to the game player.
    3. //Once in the game scene the server will share this information with all the clients.
    4. public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
    5.     {
    6.        
    7.         //This hook allows you to apply state data from the lobby-player to the game-player
    8.         //just subclass "LobbyHook" and add it to the lobby object.
    9.         print("Calling ServerSceneLoadedForPlayer");
    10.         ShipInfo si = lobbyPlayer.GetComponent<ShipInfo>();
    11.         ShipControl sc = gamePlayer.GetComponent<ShipControl>();
    12.         sc.damage = si.weaponLevel;
    13.         sc.rechargeTime = si.weaponSpeed;
    14.         sc.maxShield = si.shieldLevel;
    15.         sc.maxHull = si.hullLevel;
    16.         sc.turnSpeed = si.turnThrusters;
    17.         sc.thrusters = si.mainThrusters;
    18.         sc.currentShipIndex = si.shipType;
    19.         gamePlayer.GetComponent<Rigidbody2D>().mass = si.mass;
    20.         return true;
    21.     }
     
  2. BeforeTheLight

    BeforeTheLight

    Joined:
    Jan 7, 2011
    Posts:
    168
    Ok so I know I just posted but I was able to insert a Debug.Break() by overriding OnLobbyServerCreateGamePlayer to see this. On the first load my player objects are in the test scene when this break hits. On the second call it appears two scenes are created and the first one which contains my spawned players gets destroyed. Attempts 3,4,... Have that many more loading Test scenes when I start a new game. Let me know if I am maybe misinterpreting this or if this is really the issue I am fighting here. And if so how can I stop it from happening.

    Screen Shot 2018-10-14 at 7.07.05 AM.png
     
  3. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    There is a NetworkLobby asset on the Asset Store (from Unity) that might help you get started.
    It did have a lobby + example game back when I used it, but I think it's only lobby now.

    There are still a lot of bugs in it both the Lobby asset and the HLAPI NetworkLobbyManager. You are probably better of creating your own LobbyManager by inheriting from NetworkManager. It's not impossible to do. Have a list of lobby players. Put them in there after connecting. When all are ready then switch the scene* and spawn prefabs for each one.

    * scene switching has bugs too btw.
     
  4. BeforeTheLight

    BeforeTheLight

    Joined:
    Jan 7, 2011
    Posts:
    168
    Thanks for the advice but for anyone else having a similar issues the simple answer is I am an idiot. But here are the details. I had built this as a single match game. Everything got destroyed and everyone went back to the main menu, next match needed to be reset every time. I modded this to include the lobby and in my ignorance reused some code that got the button to set the selected ship and set the event handler. I used this button in the lobby to set the player to ready. The lobby being a do not destroy on load object kept these and every time the scene was loaded they were added again causing the scene to reload over and over resulting in the Test, Test(Loading), Test(Loading), Test(Loading) ... hierarchy I was seeing. Hope this can save someone in the future.