Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Programmer9001 · Aug 07, 2013 at 05:49 AM · multiplayernetworkconnectionlanmultiplayer networking

Trying to Connect a Remote Computer from a Different ISP

Hi! I have been struggling with network connectivity with another user. I can host the server over my LAN and it works fine, but whenever I try to connect a computer from a different ISP, it will not allow them to connect. They can see my server on a different script I wrote that shows up in a list, but cannot connect (You tell the game to refresh the server list and it brings up any servers it finds). I have port-forwarded port 80 and verified that it is open with CanYouSeeMe.org. Please help and thank you for reading this. :) Here is my code for Multiplayer Management.

 using UnityEngine;
 using System.Collections;
 
 /// <summary>
 /// This script is attached to the MultiplayerManager and it
 /// is the foundation for our multiplayer system.
 /// </summary>
 
 public class MultiplayerScript : MonoBehaviour 
 {
     //Variables Start===================================
     
     private string titleMessage = "Network Prototype";
     
     private string connectToIP  = "127.0.0.1";
     
     private int connectionPort  = 80;
     
     private bool useNAT = false;
     
     private string ipAddress;
     
     private string port;
     
     private int numberOfPlayers = 10;
     
     public string playerName;
     
     public string serverName;
     
     public string serverNameForClient;
     
     private bool iWantToSetupAServer = false;
     
     private bool iWantToConnectToAServer = false;
     
     
     // These variables are used to define the main window.
     
     private Rect connectionWindowRect;
     
     private int connectionWindowWidth = 400;
     
     private int connectionWindowHeight = 280;
     
     private int buttonHeight = 60;
     
     private int leftIndent;
     
     private int topIndent;
     
     
     //These variables are used to define the server
     //shutdown window.
     
     private Rect serverDisWindowRect;
     
     private int serverDisWindowWidth = 300;
     
     private int serverDisWindowHeight = 150;
     
     private int serverDisWindowLeftIndent = 10;
     
     private int serverDisWindowTopIndent = 10;
     
     
     //These variables are used to define the client
     //disconnect window.
     
     private Rect clientDisWindowRect;
     
     private int clientDisWindowWidth = 300;
     
     private int clientDisWindowHeight = 170;
     
     private bool showDisconnectWindow = false;
     
     // Variables End=====================================
 
     // Use this for initialization
     void Start () 
     {
         //Load the last used serverName from registry.
         //If the serverName is blank then use "Server"
         //as a default name.
         
         serverName = PlayerPrefs.GetString("serverName");
         
         if(serverName == "")
         {
             serverName = "Server";    
         }
         
         //Load the last used playerName from registry.
         //If the playerName is blank the use "Player"
         //as a default name.
         
         playerName = PlayerPrefs.GetString("playerName");
         
         if(playerName == "")
         {
             playerName = "Player";
         }
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetKeyDown(KeyCode.Escape))
         {
             showDisconnectWindow = !showDisconnectWindow;    
         }
     }
     
     void ConnectWindow(int windowID)
     {
         //Leave a gap from the header.
         
         GUILayout.Space(15);
         
         //When the player launches the game they have the option
         //to create a server or join a server. The variables
         //iWantToSetupAServer and iWantToConnectToAServer start as
         //false so that the player is presented with two buttons
         //"Setup my server" and "Connect to a server".
         
         if(iWantToSetupAServer == false && iWantToConnectToAServer == false)
         {
             if(GUILayout.Button("Setup a server", GUILayout.Height(buttonHeight)))
             {
                 iWantToSetupAServer = true;    
             }
             
             GUILayout.Space(10);
             
             if(GUILayout.Button("Connect to a server", GUILayout.Height(buttonHeight)))
             {
                 iWantToConnectToAServer = true;    
             }
             
             GUILayout.Space(10);
             
             if(Application.isWebPlayer == false && Application.isEditor == false)
             {
                 if(GUILayout.Button("Exit Prototype", GUILayout.Height(buttonHeight)))
                 {
                     Application.Quit();    
                 }
             }
         }
         
         if(iWantToSetupAServer == true)
         {
             //The user can type a name for their server into the
             //textfield.
             
             GUILayout.Label("Enter a name for your server");
             
             serverName = GUILayout.TextField(serverName);
             
             GUILayout.Space(5);
             
             //The user can type in the port number for their server
             //into the textfield. We defined the default value above in
             //the 26500.
             
             GUILayout.Label("Server Port");
             
             connectionPort = int.Parse(GUILayout.TextField(connectionPort.ToString()));
             
             GUILayout.Space(10);
             
             if(GUILayout.Button("Start my own server", GUILayout.Height(30)))
             {
                 //Create the server.
                 
                 Network.InitializeServer(numberOfPlayers, connectionPort, useNAT);
                 
                 //Save the serverName using PlayerPrefs.
                 
                 PlayerPrefs.SetString("serverName", serverName);
                 
                 iWantToSetupAServer = false;
             }
             
             if(GUILayout.Button("Go Back", GUILayout.Height(30)))
             {
                 iWantToSetupAServer = false;    
             }
         }
         
         if(iWantToConnectToAServer == true)
         {
             //The user can type their player name into the
             //textfield.
             
             GUILayout.Label("Enter your player name");
             
             playerName = GUILayout.TextField(playerName);
             
             GUILayout.Space(5);
             
             //The player can type the IP address for the server
             //that they want to connect to into the textfield.
             
             GUILayout.Label("Type in Server IP");
             
             connectToIP = GUILayout.TextField(connectToIP);
             
             GUILayout.Space(5);
             
             //The player can type in the Port number for the server
             //they want to connect to into the textfield.
             
             GUILayout.Label("Server Port");
             
             connectionPort = int.Parse(GUILayout.TextField(connectionPort.ToString()));
             
             GUILayout.Space(5);
             
             //The player clicks on this button to establish a connection.
             
             if(GUILayout.Button("Connect", GUILayout.Height(25)))
             {
                 //Ensure that the play can't join a game with an empty name.
                 
                 if(playerName == "")
                 {
                     playerName = "Player";    
                 }
                 
                 //If the player has a name that isn't empty then attempt to join
                 //the server.
                 
                 if(playerName != "")
                 {
                     //Connect to a server with the IP address contained in
                     //connectToIP and with the port number contained in
                     //connectionPort.
                     
                     Network.Connect(connectToIP, connectionPort);
                     
                     PlayerPrefs.SetString("playerName", playerName);
                 }
             }
             
             GUILayout.Space(5);
             
             if(GUILayout.Button("Go Back", GUILayout.Height(25)))
             {
                 iWantToConnectToAServer = false;    
             }
         }
     }
     
     void ServerDisconnectWindow(int windowID)
     {
         GUILayout.Label("Server name: " + serverName);
         
         //Show the number of players connected.
         
         GUILayout.Label("Number of players connected: " + Network.connections.Length);
         
         //If there is at least one connection then show the average ping.
         
         if(Network.connections.Length >= 1)
         {
             GUILayout.Label("Ping: " + Network.GetAveragePing(Network.connections[0]));    
         }
         
         //Shutdown the server if the user clicks on the Shutdown server button.
         
         if(GUILayout.Button("Shutdown server"))
         {
             Network.Disconnect();    
         }
     }
     
     void ClientDisconnectWindow(int windowID)
     {
         //Show the player the server they are connected to and the
         //average ping of their connection.
         
         GUILayout.Label("Connected to server: " + serverName);
         
         GUILayout.Label("Ping: " + Network.GetAveragePing(Network.connections[0]));
         
         GUILayout.Space(7);
         
         //The player disconnects from the server when they press the
         //Disconnect button.
         
         if(GUILayout.Button("Disconnect", GUILayout.Height(25)))
         {
             Network.Disconnect();    
         }
         
         GUILayout.Space(5);
         
         //This button allows the player using a webplayer who has gone
         //fullscreen to be able to return to the game. Pressing escape in
         //fullscreen doesn't help as that just exits fullscreen.
         
         if(GUILayout.Button("Return to Game", GUILayout.Height(25)))
         {
             showDisconnectWindow = false;    
         }
     }
     
     void OnDisconnectedFromServer()
     {
         //If a player loses the connection or leaves the scene
         //then the level is restarted on their computer.
         
         Application.LoadLevel(Application.loadedLevel);
     }
     
     void OnPlayerDisconnected(NetworkPlayer networkPlayer)
     {
         //When the player leaves the server delete them across the network
         //along with their RPCs so that other players no longer see them.
         
         Network.RemoveRPCs(networkPlayer);
         
         Network.DestroyPlayerObjects(networkPlayer);
     }
     
     void OnPlayerConnected(NetworkPlayer networkPlayer)
     {
         networkView.RPC("TellPlayerServerName", networkPlayer, serverName);    
     }
     
     void OnGUI()
     {
         //If the player is disconnected then run the ConnectWindow function.
         
         if(Network.peerType == NetworkPeerType.Disconnected)
         {
             //Determine the position of the window based on the width and
             //height of the screen. The window will be placed in the middle
             //of the screen.
             
             leftIndent = Screen.width / 2 - connectionWindowWidth / 2;
             
             topIndent = Screen.height / 2 - connectionWindowHeight / 2;
             
             connectionWindowRect = new Rect(leftIndent, topIndent, connectionWindowWidth, 
                                             connectionWindowHeight);
             
             connectionWindowRect = GUILayout.Window(0, connectionWindowRect, ConnectWindow,
                                             titleMessage);
         }
         
         //If the game is running as a server then run the ServerDisconnectWindow
         //function.
         
         if(Network.peerType == NetworkPeerType.Server)
         {
             //Defining the Rect for the server's disconnect window.
             
             serverDisWindowRect = new Rect(serverDisWindowLeftIndent, serverDisWindowTopIndent,
                                             serverDisWindowWidth, serverDisWindowHeight);
             
             serverDisWindowRect = GUILayout.Window(1, serverDisWindowRect, ServerDisconnectWindow, "");
         }
         
         //If the connection type is a client (a player) then show a window that allows
         //them to disconnect from the server.
         
         if(Network.peerType == NetworkPeerType.Client && showDisconnectWindow == true)
         {
             clientDisWindowRect = new Rect(Screen.width / 2 - clientDisWindowWidth / 2, 
                                             Screen.height / 2 - clientDisWindowHeight / 2,
                                             clientDisWindowWidth, clientDisWindowHeight);
             
             clientDisWindowRect = GUILayout.Window(1, clientDisWindowRect, ClientDisconnectWindow, "");
         }
     }
     
     //Used to tell the MultiplayerScript in connected players the serverName. Otherwise
     //players connecting wouldn't be able to see the name of the server.
     
     [RPC]
     void TellPlayerServerName (string servername)
     {
         serverName = servername;    
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slobdell · Aug 07, 2013 at 08:50 PM 0
Share

Why do you have useNAT = false? I think you will still need NAT even with port forwarding, maybe give setting it to true a try.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to solve networking issues? 0 Answers

HLAPI - Old server messages sent on reconnect 0 Answers

Which Game Objects need Network Identities and which game objects don't in order to transfer data between the server and the client? 1 Answer

Photon IK sync 2 Answers

Multiplayer on LAN 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges