- Home /
Client connects to server, but Network.peerType returns Disconnected until next click
Hey all :)
I'm kinda new with all this Server-Client programming, so I followed some tutorials which gave me a basic idea of how to make it work, but now I have this really annoying problem
When I click a login button, it should run my Network.Connect("127.0.0.1", 2602); After this I should be online with my client, which is true, because I check for connections on my Server which tells me that I have one signed on, but the wierd thing is that when I print(Network.peerType); I'm given Disconnected, even though my Server says I'm connected. Next time I click the login button, it tells me I'm Client
Might it be because of my code that takes too much time to run?
function OnGUI(){
if(!loggedIn){
if(!registering){
GUI.BeginGroup(new Rect(Screen.width/2 - 300, Screen.height/2 - 300, 600, 600));
GUI.Box(new Rect(0,0,600,600),"Gladiator Wars");
username = GUI.TextArea(new Rect(200,50,200,25),username);
password = GUI.TextArea(new Rect(200,75,200,25),password);
if(GUI.Button(new Rect(200,100,200,25), "Login")){
Connect();
print(Network.peerType);
}
}
}
}
function Connect(){
if(Network.peerType == NetworkPeerType.Disconnected){
Network.Connect("127.0.0.1", 2602);
}
if(Network.peerType == NetworkPeerType.Client){
networkView.RPC("Login", RPCMode.Server, username, password, Network.player);
}
print(Network.peerType);
}
Answer by mark03468 · Jul 03, 2012 at 05:49 PM
Network.Connect() needs some time to made connection.
Try to use :
// For Server
function OnPlayerConnected (player : NetworkPlayer)
{
networkView.RPC("RequestLoginInfo" , player , true);
}
// For Client
@RPC
function RequestLoginInfo (loggedIn : boolean)
{
networkView.RPC("Login", RPCMode.Server, username, password, Network.player);
}
or use function OnConnectedToServer () in client
Answer by gilmarhz · Oct 19, 2012 at 07:52 PM
Sorry, it's late to answer this, but google shows this link in first place when I search for "Network.peerType". For those people who came here looking for answer about this problem, it isn't a unity bug, just set "Run In Background = true" at Player Settings and "Network.peerType ==NetworkPeerType.Disconnected" is work fine.
Para quem está lendo em português, se não setar "Run In Backgroud = true" em Edit/Project Settings/Player e fizer testes locais em sua máquina conectando duas instâncias do jogo, a que tem o server estará em background quando o cliente conectar, e Network.peerType continuará == NetworkPeerType.Disconnected e os exemplos encontrados na net não funcionarão.
Espero ter ajudado.