- Home /
Question by
Alexvaldes0612 · Jul 03, 2013 at 01:16 AM ·
multiplayernetworkserver
Players can't connect to my server
So I coded this multiplayer game and when i checked with my self it worked, but i gave my friend my public ip and port so he can join and he couldn't. I port forwarded my port.
this is my NetworkManager code
#pragma strict
//ADDED FOR CROSSHAIR
var crosshairTexture : Texture2D;
var position : Rect;
static var OriginalOn = true;
//
var playerPrefab : GameObject;
var spawnObject : Transform;
//ADDED FOR ONLINE
var ipAddress : String = "127.0.0.1";
var port : int = 25565;
var maxConnections : int = 6;
//
var gameName : String = "level1";
private var refreshing : boolean = false;
private var hostData : HostData[];
function OnGUI () {
if(!Network.isClient && !Network.isServer) {
//ADDED FOR ONLINE
GUILayout.BeginHorizontal ();
ipAddress = GUILayout.TextField (ipAddress);
GUILayout.Label ("IP ADDRESS");
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
var tempPort : String;
tempPort = GUILayout.TextField (port.ToString());
port = parseInt(tempPort);
GUILayout.Label ("PORT");
GUILayout.EndHorizontal();
if(GUILayout.Button ("CONNECT")) {
print("Connecting...");
Network.Connect (ipAddress, port);
showCrosshair(); //ADDED
}
//
if (GUI.Button(Rect(Screen.width/2,Screen.height/2,100,20),"Start Server")) {
startServer();
showCrosshair(); //ADDED
}
if (GUI.Button(Rect(Screen.width/2,Screen.height/2 + 30,100,20),"Refresh Hosts")) {
Debug.Log("Refresh");
refreshHostList();
}
if(hostData) {
for(var i:int = 0; i<hostData.length; i++) {
if(GUI.Button(Rect(Screen.width/2,Screen.height/2 + 60,100,20),hostData[i].gameName)) {
Network.Connect(hostData[i]);
}
}
}
}
// ADDED FOR CROSSHAIR
if(OriginalOn == true)
{
GUI.DrawTexture(position, crosshairTexture);
}
//
}
function Update () {
if(refreshing) {
if(MasterServer.PollHostList().Length > 0) {
refreshing = false;
Debug.Log(MasterServer.PollHostList().Length);
hostData = MasterServer.PollHostList();
}
}
}
function startServer () {
// Stuff in green in for LAN not ONLINE
//Network.InitializeServer(32,25001, !Network.HavePublicAddress);
Network.InitializeServer (maxConnections, port, false);
//MasterServer.RegisterHost(gameName, "Test 1", "This is a testing room");
}
function OnServerInitialized () {
Debug.Log("server initialized");
spawnPlayer();
}
function OnConnectedToServer () {
spawnPlayer();
}
function spawnPlayer () {
Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0);
}
function OnMasterServerEvent(mse:MasterServerEvent) {
if(mse == MasterServerEvent.RegistrationSucceeded) {
Debug.Log("Registered Server");
}
}
function refreshHostList () {
MasterServer.RequestHostList(gameName);
refreshing = true;
}
//ADDED FOR CROSSHAIR
function showCrosshair () {
position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height -
crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);
}
//
Please help, thank you
Comment
Have you tried connecting using the Unity $$anonymous$$aster Server? Because if that doesn't work, then it's not a problem with your game
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Networking??? 2 Answers
Networked multiplayer with dedicated server 0 Answers
Can you create a Stand alone server for your game? 0 Answers
can i use unity and php to develop a multi player game 2 Answers