Search Unity

[UNET] Weird Bugs On Player Join.

Discussion in 'Scripting' started by DaedricWolf19, Jun 14, 2018.

  1. DaedricWolf19

    DaedricWolf19

    Joined:
    Nov 23, 2015
    Posts:
    5
    Hello, I don't really get uNET yet, but i am trying. First. My menu scene is networked, due to the nature of it and so i have came across some really weird bugs. For one, every time someone connects to the host, the game freezes up. The client gets no updates to anything and stops moving. The Host sees two players spawn together and not move. The whole thing just freezes up. I am running the menu on an obscure port and ip address, this then is changed and everything is reset upon button press. I have no clue what is happening. I sometimes get a 'host bind {-1]' error as well as sometimes getting a warning of 'AddPlayer: player object already exists for playerControllerID'


    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7. using UnityEngine.SceneManagement;
    8.  
    9. public class GameNetworkManager : NetworkManager {
    10.  
    11. public int connections = 0;
    12.  
    13. public GameObject menuPlayer;
    14.  
    15. public override void OnServerConnect(NetworkConnection Conn)
    16. {
    17. connections += 1;
    18. }
    19.  
    20.  
    21. // Called on the client when connected to a server.
    22. public override void OnClientConnect(NetworkConnection conn)
    23. {
    24. //base.OnClientConnect(conn);
    25.  
    26. if(SceneManager.GetActiveScene().buildIndex == 0)
    27. ClientScene.AddPlayer(conn, 0);
    28.  
    29. }
    30.  
    31. public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    32. {
    33.  
    34. if(conn.connectionId < 0)
    35. {
    36. return;
    37. }
    38.  
    39. GameObject player = Instantiate(menuPlayer);
    40. player.transform.position = GameObject.FindGameObjectWithTag("Spawn").transform.position;
    41.  
    42. Debug.Log("Spawned " + conn.isReady);
    43. Debug.Log("COUNT: " + NetworkServer.connections.Count);
    44.  
    45. // if(NetworkServer.connections)
    46. NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    47. }
    48.  
    49. public override void OnClientSceneChanged(NetworkConnection conn)
    50. {
    51. // base.OnClientSceneChanged(conn);
    52.  
    53. if (SceneManager.GetActiveScene().buildIndex != 0)
    54. {
    55. ClientScene.AddPlayer(conn, 0);
    56. }
    57. }
    58.  
    59.  
    60. public void ChangeScene(string sceneName)
    61. {
    62. NetworkManager.singleton.ServerChangeScene(sceneName);
    63. }
    64.  
    65. public void RestartHost(int port)
    66. {
    67. NetworkManager.singleton.StopHost();
    68. NetworkManager.singleton.networkPort = port;
    69. NetworkManager.singleton.StartHost();
    70. }
    71.  
    72. public void StartupHost(int port)
    73. {
    74. NetworkManager.singleton.networkPort = port;
    75. NetworkManager.singleton.StartHost();
    76. }
    77.  
    78. public void JoinGame(string address, int port)
    79. {
    80. NetworkManager.singleton.StopHost();
    81. NetworkManager.singleton.networkAddress = address;
    82. NetworkManager.singleton.networkPort = port;
    83. NetworkManager.singleton.StartClient();
    84. }
    85.  
    86.  
    87. public void HostSetupSimple(string sceneName)
    88. {
    89. NetworkManager.singleton.networkAddress = GameObject.FindGameObjectWithTag("Computer").GetComponentInChildren<TMPro.TMP_InputField>().text;
    90.  
    91. if(GameObject.FindGameObjectWithTag("Computer").GetComponentInChildren<TMPro.TMP_InputField>().text == "")
    92. {
    93. NetworkManager.singleton.networkAddress = "localhost";
    94. }
    95.  
    96. NetworkManager.singleton.StopHost();
    97. NetworkServer.Reset();
    98. // NetworkServer.DisconnectAll();
    99. NetworkManager.singleton.networkPort = 7777;
    100.  
    101. NetworkManager.singleton.StartHost();
    102. NetworkManager.singleton.ServerChangeScene(sceneName);
    103. }
    104.  
    105.  
    106. public void JoinSetupSimple(string sceneName)
    107. {
    108. NetworkManager.singleton.networkAddress = GameObject.FindGameObjectWithTag("Computer").GetComponentInChildren<TMPro.TMP_InputField>().text;
    109.  
    110. if (GameObject.FindGameObjectWithTag("Computer").GetComponentInChildren<TMPro.TMP_InputField>().text == "")
    111. {
    112. NetworkManager.singleton.networkAddress = "localhost";
    113. }
    114.  
    115.  
    116. NetworkManager.singleton.StopHost();
    117. NetworkManager.singleton.networkPort = 7777;
    118. //NetworkServer.Reset();
    119. // NetworkServer.DisconnectAll();
    120. // NetworkServer.Shutdown();
    121. NetworkManager.singleton.StartClient();
    122. }
    123.  
    124. }
    125.  
    126.  
    127.  

    Thanks.