Search Unity

isNetworkActive always false !

Discussion in 'Multiplayer' started by Oberheim, Jul 8, 2021.

  1. Oberheim

    Oberheim

    Joined:
    Jun 4, 2020
    Posts:
    24
    Hi all !

    I am having a massive headache and I really hope someone can help me out with this.
    I am trying to Auto-connect players to the game.
    So I am trying to check from the client side if the server is active, and decide if I connect as host or client.
    After trying all the solutions out there (ping, register handler...) It seems that NetworkManager.isNetworkActive is exactly what i'm looking for.
    UNFORTUNATELY, when I Debug.Log(NetworkManager.isNetworkActive) it is ALWAYS false although server is running fine and my player always connects as host.

    This is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Mirror;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using System.Net.Sockets;
    7. using System.Threading;
    8.  
    9.  
    10. namespace Turnbased {
    11.  
    12.     public class AutoHostClient : MonoBehaviour {
    13.  
    14.         public string ip = "13.36.171.251";
    15.  
    16.         [SerializeField] NetworkManager networkManager;
    17.  
    18.         void Start ()
    19.         {
    20.  
    21.             networkManager = GameObject.FindObjectOfType<NetworkManager>();
    22.  
    23.             if (!Application.isBatchMode) {
    24.  
    25.                 if (Application.internetReachability == NetworkReachability.NotReachable)  //check if device is connected to the internet
    26.                 {
    27.                     Debug.Log("Error. Check internet connection!");                  
    28.                     GameObject.Find("WIFI").SetActive(true);                               //if not, shows a disconnected wifi sign
    29.                     networkManager.StartHost();                                            // if not, start as host
    30.                 }
    31.                 else
    32.                 {
    33.                     Debug.Log(NetworkManager.isNetworkActive);            
    34.  
    35.                     if (!NetworkManager.isNetworkActive)                      //Check is server is running
    36.                     {
    37.                         Debug.Log("Error. Check connection with server!");
    38.                         GameObject.Find("WIFI").SetActive(true);              //if not, shows a "no wifi" sign
    39.                         networkManager.StartHost();                           //if not, start as host.
    40.                     }
    41.                     else
    42.                     {
    43.                         Debug.Log("Connexion to server successful!");      
    44.                         GameObject.Find("WIFI").SetActive(false);             //hide "no wifi" sign
    45.                         networkManager.StartClient();                         //connect to server as client.
    46.                     }
    47.  
    48.                 }
    49.  
    50.  
    51.             } else
    52.                 Debug.Log($"=== Server Build ===");
    53.  
    54.         }
    55.  
    56.     }
    57.  
    58. }
    This is script is attached to a GameObject on my scene without network ID.

    I don't know what is wrong with this. I can't believe i'm getting so stuck on such a basic issue.
    Your help would be much much much appreciated!
    <3