- Home /
cant connect to server
hello, I was following this tutorial to create a multiplayer game. everything else works fine exept joining the server, here is the code:
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
public string uniqueGameName;
public int playerCount;
private HostData[] hostList;
public GameObject playerPrefab;
public Transform[] spawnPoint = new Transform[2];
void StartServer ()
{
Network.InitializeServer(4, 25000, !Network.HavePublicAddress());
MasterServer.RegisterHost(uniqueGameName, "MyGame");
}
void OnServerInitialized ()
{
Debug.Log("Server Initialized");
SpawnPlayer();
}
void OnConnectedToServer()
{
SpawnPlayer();
print("connected");
}
void RefreshHostList ()
{
MasterServer.RequestHostList(uniqueGameName);
}
void JoinServer (HostData myHostData)
{
Network.Connect(myHostData);
}
void OnMasterServerEvent (MasterServerEvent msEvent)
{
if (msEvent == MasterServerEvent.HostListReceived)
hostList = MasterServer.PollHostList();
}
void SpawnPlayer ()
{
Network.Instantiate(playerPrefab, spawnPoint[0].position, Quaternion.identity, 0);
}
void OnPlayerConnected()
{
playerCount++;
print ("PlayerConnected");
}
void OnGUI ()
{
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(new Rect(20, 20, 100, 20), "Start Server")) StartServer();
if (GUI.Button(new Rect(20, 45, 100, 20), "Refresh")) RefreshHostList();
if (hostList != null)
{
for (int i = 0; i < hostList.Length; i++)
{
if (GUI.Button(new Rect(125, 20 + (45 * i), 100, 20), hostList[i].gameName)) JoinServer(hostList[i]);
}
}
}
}
}
I`ve looked over it a million times but I cant find what is wrong. I tied initializing the game on a stand alone to check for connection error messages in the unity edit, and tho the game does appear on the list there is no message nor does the player spawn. Im almost sure it something stupid but I cant find it. pls tell me any theorys you have on what it could be. Thank you.
Answer by jfjordanfarr · Jul 03, 2015 at 07:29 AM
I too have followed this tutorial.
As far as I know, I'm getting the same error because Unity is likely taking down its old server(s) for matchmaking. I couldn't figure out how to compile and run my own server from my computer, so I stopped reaching out to the Unity server, registering a name, and checking it. I just set myself up to connect directly to an IP address.
I'm waiting for the new Unity 5.x networking to show up to us non-beta users before worrying too much more about networking. I know the old network view components are going to be deprecated and I'll have to redo the networking soon anyway. Unity is also offering a different matchmaking service for NAT hole punching and whatnot with the new networking (see http://unity3d.com/services/multiplayer).
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
photon marco polo tutorial issue with "peercreated" 1 Answer
Multiplayer game in webplayer not working on server 1 Answer
can i make a matchmaking without choose server 1 Answer
Multiplayer not working 1 Answer