I have this simple c# script that is attached to my main camera object.(Included below) I run two different instances, one as a server and the other as a client.
I start the server first. The server Network.peerType changes properly. Then I start the client. I type in the proper ipaddress and port and hit connect. There are no network errors, but the Network.peerType never changes on the client and the OnConnectedToServer function never gets called. However everytime I hit the Connect button, the OnPlayerConnect on the server gets called. So there's some type of client server communication. It just seems like the client is missing something.
I'm stuck and I really appreciate the help!
Thanks :)
//Code Start
using UnityEngine;
using System.Collections;
public class ConnectionGUI : MonoBehaviour {
private string remoteIp = "127.0.0.1";
private int remotePort = 25000;
private int listenPort = 25000;
private bool useNAT = false;
private string yourIP = "";
private string yourPort = "";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
if(Network.peerType == NetworkPeerType.Disconnected){
//Not connected
if(GUI.Button(new Rect(10,10,100,30),"Connect"))
{
//Connecting to the server
Debug.Log(remoteIp);
Debug.Log(remotePort);
NetworkConnectionError ne = Network.Connect(remoteIp,remotePort);
Debug.Log(ne.ToString());
Debug.Log(Network.peerType);
}
if(GUI.Button(new Rect(10,50,100,30),"Start Server")){
//Creating server
Network.InitializeServer(32,listenPort, useNAT);
Object[] gameObjects = FindObjectsOfType(typeof(GameObject));
foreach(GameObject go in gameObjects){
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
}
remoteIp = GUI.TextField(new Rect(120,10,100,20),remoteIp);
remotePort = int.Parse(GUI.TextField(new Rect(230,10,40,20),remotePort.ToString()));
}else{
//Getting your ip address and port
string ipaddress = Network.player.ipAddress;
string port = Network.player.port.ToString();
GUI.Label(new Rect(140,20,250,40),"IP Address:"+ipaddress+":"+port);
if(GUI.Button(new Rect(10,10,100,50),"Disconnect"))
{
//Disconnect from the server
Network.Disconnect(200);
}
}
}
void OnConnectedToServer(){
Debug.Log("Connected to Server");
Object[] gameObjects = FindObjectsOfType(typeof(GameObject));
foreach(GameObject go in gameObjects){
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
}
void OnPlayerConnected(){
Debug.Log("Player connected");
}
}
asked
Apr 03 '11 at 05:42 AM
Jason Jolley
105
●
7
●
7
●
14