Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I manually start a server with a NetworkManager?

Discussion in 'Multiplayer' started by maikklein, Jul 18, 2015.

  1. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    I want to create a script that automatically starts the server in the editor and automatically connects the clients from a standalone build to the server.

    All I need to know is what the NetworkManagerHUD is doing under the hood. Which functions are getting called when I press LanServerOnly/LanCient?
     
  2. Lelon

    Lelon

    Joined:
    May 24, 2015
    Posts:
    79
    maikklein likes this.
  3. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    That is super helpful thank you.


    I still have some trouble. Here is what I did:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(NetworkManager))]
    6. public class Connect : MonoBehaviour {
    7.  
    8.     bool isEditor = false;
    9.     NetworkManager manager;
    10.     // Use this for initialization
    11.     void Awake () {
    12.         manager = GetComponent<NetworkManager>();
    13.         #if UNITY_EDITOR
    14.             isEditor = true;
    15.         #endif
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update () {
    20.         if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null && isEditor)
    21.         {
    22.             manager.StartHost();
    23.         }
    24.         if (!NetworkClient.active && !NetworkServer.active && manager.matchMaker == null && !isEditor)
    25.         {
    26.             manager.StartClient();
    27.         }
    28.         if (NetworkClient.active && !ClientScene.ready)
    29.         {
    30.             ClientScene.Ready(manager.client.connection);
    31.  
    32.             if (ClientScene.localPlayers.Count == 0)
    33.             {
    34.                 ClientScene.AddPlayer(0);
    35.             }
    36.         }
    37.     }
    38. }
    39.  
    It works with the server but the clients are unable to connect. After some debugging I found out that

    Code (CSharp):
    1. //manager.client.connection is null
    2. ClientScene.Ready(manager.client.connection);
    'manager.client.connection' is null for all clients and I am not sure why that happens.
     
  4. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    Okay this is super weird, I am unable to automatically connect the client to server. It works if I put the client ready logic behind a button.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. [RequireComponent(typeof(NetworkManager))]
    5. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    6. public class Connect : MonoBehaviour {
    7.  
    8.     public NetworkManager manager;
    9.     // Use this for initialization
    10.     bool isEditor = false;
    11.     void Start () {
    12.         manager = GetComponent<NetworkManager>();
    13.         #if UNITY_EDITOR
    14.             isEditor = true;
    15.         #endif
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void OnGUI () {
    20.         if (!NetworkClient.active && !NetworkServer.active && isEditor)
    21.         {
    22.             manager.StartHost();
    23.         }
    24.         if (!NetworkClient.active && !NetworkServer.active && !isEditor)
    25.         {
    26.             manager.StartClient();
    27.         }
    28.         if (NetworkClient.active && !ClientScene.ready)
    29.         {
    30.             if (GUI.Button(new Rect(0, 400, 200, 20), "Client Ready"))
    31.             {
    32.                 ClientScene.Ready(manager.client.connection);
    33.  
    34.                 if (ClientScene.localPlayers.Count == 0)
    35.                 {
    36.                     ClientScene.AddPlayer(0);
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }
    42.  
    But if I remove the button it doesn't work anymore.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. [RequireComponent(typeof(NetworkManager))]
    5. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    6. public class Connect : MonoBehaviour
    7. {
    8.  
    9.     public NetworkManager manager;
    10.     // Use this for initialization
    11.     bool isEditor = false;
    12.     void Start()
    13.     {
    14.         manager = GetComponent<NetworkManager>();
    15. #if UNITY_EDITOR
    16.         isEditor = true;
    17. #endif
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void OnGUI()
    22.     {
    23.         if (!NetworkClient.active && !NetworkServer.active && isEditor)
    24.         {
    25.             manager.StartHost();
    26.         }
    27.         if (!NetworkClient.active && !NetworkServer.active && !isEditor)
    28.         {
    29.             manager.StartClient();
    30.         }
    31.         if (NetworkClient.active && !ClientScene.ready)
    32.         {
    33.             ClientScene.Ready(manager.client.connection);
    34.  
    35.             if (ClientScene.localPlayers.Count == 0)
    36.             {
    37.                 ClientScene.AddPlayer(0);
    38.             }
    39.         }
    40.     }
    41. }
    42.  
    Any idea?
     
  5. Lelon

    Lelon

    Joined:
    May 24, 2015
    Posts:
    79
    I believe that's because you are using OnGUI(), OnGUI is only used for the GUI stuff. If you want a normal logic loop without GUI use Update() or FixedUpdate().
     
  6. maikklein

    maikklein

    Joined:
    Jun 16, 2015
    Posts:
    33
    I found the bug


    Code (CSharp):
    1.         if (NetworkClient.active && !ClientScene.ready && manager.client.connection != null)
    2.         {
    3.             ClientScene.Ready(manager.client.connection);
    4.  
    5.             if (ClientScene.localPlayers.Count == 0)
    6.             {
    7.                 ClientScene.AddPlayer(0);
    8.             }
    9.         }
    Better make sure never to call ClientScene.Ready with null or things get weird.

    Btw this should probably be added to the NetworkManagerHUD.
     
  7. JesusChrist17

    JesusChrist17

    Joined:
    Sep 17, 2016
    Posts:
    12
    hey guys, do you know how to give a name to the host when i am going to start host? (LAN), that is becouse after this i use network discovery then it shows is a IP, i want to show the name, not ip, help!