- Home /
Multiplayer not working
Hi I am trying to set up a multiplayer game. So i followed a tutorial (http://cgcookie.com/unity/2011/12/20/introduction-to-networking-in-unity/) and made a first test. Now i can connect with myself and it works just fine, but when my friend tries to connect, things get weird. Usually one player starts a server, and the other clicks the refresh hosts button. Then a button appears for each available host/server. When i test it on my own, i can connect by clicking on this new button. When my frined tries it however, nothing happens. The same situation when he hosts and i try to connect.
This is the full Network Script:
#pragma strict
var gameName : String = "TestMultiplayerGame123321";
private var refreshing : boolean;
private var hostData : HostData[];
var Button01 : GameObject;
var Button02 : GameObject;
var player : GameObject;
var spawn : GameObject;
//Start Server
function StartServer()
{
Network.InitializeServer(2, 25000, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, "Test", "This is a test");
}
//Messages
function OnServerInitialized()
{
Debug.Log("Server Initialized");
spawnPlayer();
}
function OnConnectedToServer()
{
spawnPlayer();
}
//Spawning a Player
function spawnPlayer()
{
Network.Instantiate(player, spawn.transform.position, Quaternion.identity, 0);
}
function OnMasterServerEvent(mse : MasterServerEvent)
{
if (mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Registration Succeeded");
}
}
//Refreshing Hosts etc.
function RefreshHostList()
{
MasterServer.RequestHostList(gameName);
refreshing = true;
}
function Update()
{
if (refreshing)
{
if(MasterServer.PollHostList().Length > 0)
{
refreshing = false;
Debug.Log(MasterServer.PollHostList().Length);
hostData = MasterServer.PollHostList();
//ListHosts();
}
}
}
function OnGUI()
{
if (!Network.isClient && !Network.isServer)
{
if (hostData != null)
{
for (var i : int = 0; i < hostData.length; i++)
{
if(GUI.Button(Rect(Screen.width * 0.5, Screen.height * 0.1 + i/2, 50, 50), hostData[i].gameName))
{
Network.Connect(hostData[i]);
}
}
}
}
else
{
Button01.active = false;
Button02.active = false;
}
}
Can anyone help me?
Thanks in advance :-)
Btw, don´t let the usage of both the new GUI system and the old one confuse you. I was experiencing some problems so i alternated between both for test purposes
Thanks for the quick answer :-). What do you mean? What would portforwarding mean?
By default, your router will block connections that go through a certain internet 'port'. It's like a gate for internet traffic.
Find your route on the guide and follow the instructions. Port forwarding guide
If you need help pm me :)
Answer by Noob_Vulcan · Feb 07, 2015 at 12:15 PM
Since you are using Unity Master Server ,there is possibility that it is down(its a test server). So rather can connecting via Master Server connect directly via IP addresses
private void StartServer ()
{
Network.InitializeServer (1, 25003, true);
MasterServer.RegisterHost (_typeName, _gameName);
Debug.Log ("My Ip " + Network.player.ipAddress);
}
#region ClickEvents
public void OnClick_StartServer ()
{
if (!Network.isServer && !Network.isClient)
StartServer ();
else
Debug.Log ("Server Running...");
}
public void OnClick_ConnectServer ()
{
Network.Connect (_serverPcIpAddress, 25003);
}
#endregion
You can get Your IP address via Network.player.ipAddress
. Just ask your friend to connect to this address.
For now, you will have to hard code ServerPCAddress.
Hope this helps :)
Thanks for the answer. Still doesn´t work unfortunately :-(.
@$$anonymous$$eral : can u post ur whole code ..what u did