Search Unity

Problems establishing online multiplayer

Discussion in 'Connected Games' started by Dawnreaver, Sep 21, 2012.

  1. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Hey Folks ^_^

    I'm having a hard time getting online multi player up and running. I followed a tutorial on UnityCookie.com. For some reason I'm unable to connect to the other Users "room". When I try to connect Unity throws the error message: "Are you sure you are able to connect to the Server?"

    I've been reading up stuff on this issue for about a week now and I cannot find any solution. I'm using a UMTS stick to connect to the internet, so there is no router that blocks the connections. I'm able to play Unity Multiplayer games online so my Firewall is not blocking in coming connections either.

    Code (csharp):
    1.  
    2. UnityCookie.com
    3.  
    4.  
    5. #pragma strict
    6. var playerPrefab : GameObject;
    7. var spawnPoint : Transform;
    8. var gameName : String = "TestGame";
    9.  
    10. private var refreshing : boolean;
    11. private var hostData : HostData[];
    12. private var buttonX : float;
    13. private var buttonY : float;
    14. private var buttonHeight : float;
    15. private var buttonWidth : float;
    16.  
    17. // menu booleans
    18.  
    19. var menuMain : boolean = true;
    20. var menuCreateServer : boolean = false;
    21. var menuOptions : boolean = false;
    22. var menuHelp : boolean = false;
    23. var inGame : boolean = false;
    24.  
    25.  
    26. // menu windows
    27.  
    28. var windowMainMenu : Rect = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    29. var windowCreateServer : Rect = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    30. var windowOptions : Rect = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    31. var windowHelp : Rect = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    32.  
    33. // window specs
    34. var screenOffset = 20;
    35. var screenHeight : float = 550;
    36. var screenWidth : float = 670;
    37. private var menuWidth = screenWidth - screenOffset*2;
    38. private var menuHeight = screenHeight -screenOffset*2;
    39.  
    40. //scrollview
    41.  
    42. var scrollPosition : Vector2 = Vector2(20,150);
    43.  
    44. // host options
    45. var advancedOptions : boolean = false;
    46. var hostedGameName: String = "Enter name  here ...";
    47. var portString : String = "Enter port here...";
    48. var maxPlayers : int = 8;
    49. static var port : int = 80;
    50.  
    51. function Start ()
    52.     {
    53.         buttonX = ((menuWidth/2)-50);
    54.         buttonY = 30;
    55.         buttonHeight = 40;
    56.         buttonWidth = 100;
    57.        
    58.         windowMainMenu = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    59.         windowCreateServer = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    60.         windowOptions = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    61.         windowHelp = Rect(screenOffset,screenOffset,screenWidth-screenOffset*2,screenHeight-screenOffset*2);
    62.     }
    63.  
    64. function StartServer()
    65.     {
    66.         var useNat = !Network.HavePublicAddress;
    67.         Network.InitializeServer(maxPlayers,25001,useNat);
    68.         //Network.InitializeServer(maxPlayers,port,!Network.HavePublicAddress);
    69.         MasterServer.RegisterHost(gameName, hostedGameName, "This is a TestGame");
    70.     }
    71. function refreshHostList()
    72.     {
    73.         MasterServer.RequestHostList(gameName);
    74.         refreshing = true;
    75.         //MasterServer.PollHostList();
    76.        
    77.     }
    78.    
    79. function Update ()
    80.     {
    81.        
    82.         //Debug.Log(port);
    83.         if(refreshing)
    84.             {
    85.                 if(MasterServer.PollHostList().Length > 0)
    86.                     {
    87.                         refreshing = false;
    88.                         Debug.Log(MasterServer.PollHostList().Length);
    89.                         hostData = MasterServer.PollHostList();
    90.                     }
    91.             }
    92.     }
    93.  
    94. function spawnPlayer()
    95.     {
    96.         Debug.Log("Spawn");
    97.         Network.Instantiate(playerPrefab,spawnPoint.position, Quaternion.identity,0);
    98.     }
    99.  
    100. // Messages
    101. function OnServerInitialized()
    102.     {
    103.         Debug.Log("Server Initialized!");
    104.         spawnPlayer();
    105.     }
    106.  
    107. function OnConnectedToServer()
    108.     {
    109.         spawnPlayer();
    110.     }
    111. // Debug if unable to connect to server
    112. function OnFailedToConnect(error: NetworkConnectionError)
    113.     {
    114.         Debug.Log("Could not connect to server: "+ error);
    115.     }
    116. // Debug ifconnection to server is lost    
    117. function OnDisconnectedFromServer(info : NetworkDisconnection) {
    118.     if (Network.isServer)
    119.         {
    120.             Debug.Log("Local server connection disconnected");
    121.             menuMain = true;
    122.             menuCreateServer = false;
    123.             menuOptions = false;
    124.             menuHelp = false;
    125.             inGame = false;
    126.         }
    127.     else
    128.         {
    129.             if (info == NetworkDisconnection.LostConnection)
    130.                 {
    131.                     Debug.Log("Lost connection to the server");
    132.                     menuMain = true;
    133.                     menuCreateServer = false;
    134.                     menuOptions = false;
    135.                        menuHelp = false;
    136.                     inGame = false;
    137.                 }
    138.             else
    139.                 {
    140.                     Debug.Log("Successfully diconnected from the server");
    141.                     menuMain = true;
    142.                     menuCreateServer = false;
    143.                         menuOptions = false;
    144.                     menuHelp = false;
    145.                     inGame = false;
    146.                 }
    147.         }
    148. }    
    149.  
    150. function OnMasterServerEvent(mse : MasterServerEvent)
    151.     {
    152.         if(mse == MasterServerEvent.RegistrationSucceeded)
    153.             {
    154.                 Debug.Log("Registered Server");
    155.             }
    156.     }
    157.    
    158. function OnPlayerDisconnected(player : NetworkPlayer)
    159.     {
    160.         Network.RemoveRPCs(player,0);
    161.         Network.DestroyPlayerObjects(player);
    162.     }
    163. // GUI
    164. function OnGUI()    
    165.     {
    166. // menu logic
    167.     if (menuMain  !menuCreateServer  !menuOptions  !menuHelp  !inGame)
    168.         {
    169.             windowMainMenu = GUI.Window (1, windowMainMenu, WindowMainMenu,"Main Menu");
    170.         }
    171.     if (!menuMain  menuCreateServer  !menuOptions  !menuHelp  !inGame)
    172.         {
    173.             windowCreateServer = GUI.Window (2, windowCreateServer, WindowCreateServer,"Create a Game");
    174.         }
    175.     if (!menuMain  !menuCreateServer  menuOptions  !menuHelp  !inGame)
    176.         {
    177.             windowOptions = GUI.Window (3, windowOptions, WindowOptions,"Options");
    178.         }
    179.     if (!menuMain  !menuCreateServer  !menuOptions  menuHelp  !inGame)
    180.         {
    181.             windowHelp = GUI.Window (4, windowHelp, WindowHelp,"How to play");
    182.         }
    183.     // ingame HUD
    184.     if (!menuMain  !menuCreateServer  !menuOptions  !menuHelp  inGame)
    185.         {
    186.         }
    187.  
    188.     }
    189.  
    190. // main menu    
    191. function WindowMainMenu(windowID :int)
    192.     {
    193.  
    194.    
    195.     if(!Network.isClient  !Network.isServer)
    196.         {
    197.             if(GUI.Button(Rect(buttonX,buttonY,buttonWidth,buttonHeight),"Create Game"))
    198.                 {    
    199.                     //Debug.Log("Starting Server");
    200.                     //StartServer();
    201.                     menuMain = false;
    202.                     menuCreateServer = true;
    203.                    
    204.                 }
    205.        
    206.             if(GUI.Button(Rect(buttonX,buttonY*1.2+buttonHeight,buttonWidth,buttonHeight),"Refresh Server"))
    207.                 {
    208.                     Debug.Log("Refreshing Server");
    209.                     refreshHostList();
    210.                 }
    211.             GUI.Box(Rect(20,150,590,200),"");
    212.             if(hostData)
    213.                 {
    214.                 // scrollview for found servers
    215.                
    216.                 GUILayout.BeginScrollView(scrollPosition,GUILayout.Width(590),GUILayout.Height(600));
    217.                
    218.                     for ( var i : int = 0; i < hostData.length; i++)
    219.                         {
    220.                             GUI.Box(Rect(20,150+buttonHeight*i,550-buttonWidth,buttonHeight),"Game name: "+hostData[i].gameName+" - Players connected: "+hostData[i].connectedPlayers+"\n"+" IP:"+hostData[i].ip+" Port:"+hostData[i].port);
    221.                             if(GUI.Button(Rect(570-buttonWidth,150+(buttonHeight*i),buttonWidth,buttonHeight),"Join"))
    222.                                 {
    223.                                     Network.Connect(hostData[i]);
    224.                                     inGame = true;
    225.                                 }
    226.                         }
    227.                   GUILayout.EndScrollView();
    228.                   //-> End of ScrollView
    229.                 }
    230.             if(GUI.Button(Rect(510,450,100,40),"Options"))
    231.                 {
    232.                     menuMain = false;
    233.                     menuOptions = true;
    234.                 }
    235.             if(GUI.Button(Rect(20,450,100,40),"How to Play"))
    236.                 {
    237.                     menuMain = false;
    238.                     menuHelp = true;
    239.                 }
    240.         }
    241.     }
    242. // create a server menu
    243. function WindowCreateServer(windowID :int)
    244.     {
    245.     // name the game
    246.     GUI.Label(Rect(20,150,120,40),"Name your game: ");
    247.     hostedGameName = GUI.TextField(Rect(140,150,200,40),hostedGameName,32);
    248.     // adjust number of Players
    249.     GUI.Label(Rect(20,200,120,40),"Numbers of players: ");
    250.     if(GUI.Button(Rect(140,200,40,40),"-")  maxPlayers > 1)
    251.         {
    252.             maxPlayers -= 1;
    253.         }
    254.     GUI.Label(Rect(185,200,80,40),""+maxPlayers);
    255.     if(GUI.Button(Rect(270,200,40,40),"+")  maxPlayers < 16)
    256.         {
    257.             maxPlayers +=1;
    258.         }
    259.  
    260. //            GUI.Label(Rect(20,300,120,40),"Game - Port: ");
    261. //            portString = GUI.TextField(Rect(140,300,120,40),""+port);
    262.  
    263.    
    264.         if(GUI.Button(Rect(menuWidth/2-30,menuHeight-100,100,40),"Start Game"))
    265.             {
    266.                 StartServer();
    267.                 menuCreateServer = false;
    268.                 inGame = true;
    269.                
    270.             }
    271.         if(GUI.Button(Rect(menuWidth/2-30,menuHeight-50,100,40),"Back"))
    272.             {
    273.                 menuCreateServer = false;
    274.                 menuMain = true;
    275.             }
    276.     }
    277. // options menu
    278. function WindowOptions(windowID :int)
    279.     {
    280.         GUI.Label(Rect(20,20,120,40),"Player color");
    281.         playerControlls.redValue = GUI.HorizontalSlider(Rect(20,70,100,20),playerControlls.redValue,0,1);
    282.         playerControlls.blueValue = GUI.HorizontalSlider(Rect(20,110,100,20),playerControlls.blueValue,0,1);
    283.         playerControlls.greenValue = GUI.HorizontalSlider(Rect(20,150,100,20),playerControlls.greenValue,0,1);
    284.        
    285.         GUI.Label(Rect(20,200,120,40),"Name your player: ");
    286.         playerControlls.playerName= GUI.TextField(Rect(140,200,200,40),playerControlls.playerName,32);
    287.        
    288.         if(GUI.Button(Rect(menuWidth/2-50,menuHeight-50,100,40),"Back"))
    289.             {
    290.                 menuOptions = false;
    291.                 menuMain = true;
    292.             }
    293.     }
    294. // helpmenu
    295. function WindowHelp(windowID :int)
    296.     {
    297.         if(GUI.Button(Rect(menuWidth/2-30,menuHeight-50,100,40),"Back"))
    298.             {
    299.                 menuHelp = false;
    300.                 menuMain = true;
    301.             }
    302.     }
    303.  
    This is the code I'm using. It is attached to a single object in the scene. Sorry that the code is so long but it is basically the multiplayer menu.

    Could you have a look on how I handle the Server functions and tell me whether I made a mistake?

    Cheers!

    Dawnreaver
     
  2. altair2020

    altair2020

    Joined:
    Mar 6, 2011
    Posts:
    28
    Hi mate,

    Where is the other server physically located ?

    Is it on your LAN or localPC or over Public IP ?
     
  3. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Sorted it out :) The cause for it not to work was the port forwarding :) Cheers!