Search Unity

How to change NetworkManager to NetworkLobbyManager

Discussion in 'Multiplayer' started by quiet00903, Jul 31, 2017.

  1. quiet00903

    quiet00903

    Joined:
    May 18, 2017
    Posts:
    19
    Hi everyone! I'm recently learning about the UNET. By now I have made a basic multiplayer game using NetworkManager. Today I read the content about NetworkLobbyManager of Unity Manual and I try to use the NetworkLobbyManager to manage my game. Here comes to some problems when I do it. Firstly, my NetworkManager play a role of deciding my player objects' start positions and assigning several spawning position to each player object. So I write a custom class extend the NetworkManager like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class MyManager : NetworkManager
    8. {
    9.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    10.     {
    11.         GameObject playerObj = Instantiate(playerPrefab);
    12.         playerObj.transform.position = startPositions[FindObjectsOfType<Player>().Length - 1].position;
    13.         playerObj.GetComponent<PlayerStatus>().MaxLife = 100f;
    14.         playerObj.GetComponent<PlayerStatus>().CurrentLife = 100f;
    15.  
    16.         //Assigning the SpawnPositions
    17.         GameObject[] spawns = GameObject.FindGameObjectsWithTag("Respawn");
    18.         Player player = playerObj.GetComponent<Player>();
    19.         for (int i = 0;i < spawns.Length;i++)
    20.         {
    21.             if (spawns[i].transform.position.x * player.transform.position.x > 0)
    22.             {
    23.                 player.spawnTransfoms.Add(spawns[i].transform);
    24.             }
    25.         }
    26.  
    27.         NetworkServer.AddPlayerForConnection(conn, playerObj, playerControllerId);
    28.     }
    29. }
    30.  
    Because I know little about NetworkLobbyManager, I want to change MyManager to NetworkManager directly, create two scenes (one called Lobby , another called Game) and the I met some problems. There are three situations: 1. I keep MyManager existed in Game scene and I Add NetworkManager and HUD to the Lobby scene like what unity maual does. I found when I enter the game from the Lobby Scene, the NetworkManager in Game scenes seems to be destroyed and my player object can't initialize properly. 2. I change MyManager to extend NetworkLobbyManager keeping code still the same. Then I delete the NetworkManager in Game scenes and add MyManager to Lobby scene. I found that the OnServerAddPlayer only be called when I enter the Lobby scene and didn't be called when I enter Game scene. MyManager worked properly when I used without NetworkLobbyManager, however, it doesn't work as I expected now. 3. I have no choice but to use OnStartServer to initialize my player object and I don't think it's a proper way.
    So my question is :
    Why NetworkManager seems to be destroyed when I have a NetworkLobbyManager by which I enter the Game scene?
    What happened when I met situation 2, why OnServerAddPlayer didn't work as I expected?
    Is there a correct function should I override when I enter the Game scene and initialize my player object?
    When I comes to the 3th situation, there is another problem: The spawnPosition in the Game scene is added NetworkIdentity Component, and the FindObjectsWithTag("Respawn") can return the GameObjects as I expected When I use MyManager : NetwrokManager. Howerver, when I use the same code in OnStartServer, the local client can't get the SpawnPositon but the remote client can get it. So I have to try to find it again when the SpawnPosition becomes null. How it becomes like this?

    Thanks in advance, I'll appreciate if there's anyone giving any suggestion! :)