Server and clients keep reconnecting on disconnect

Whenever I hit “disconnect” on either the server or my clients the server or client reconnects as if it is connecting for the first time. I have the code running in an Awake() function, which I thought only runs once when the script instance is loaded.
In my code the script gets a value for username from another game object.

Anybody have any ideas?

Thanks!

var remoteIP = "127.0.0.1";
var remotePort = 25001;
var listenPort = 25000;
var useNAT = false;

private var username : String;

function Awake() 
{
	//if there's a MasterServer running, then we won't run this script
	if (FindObjectOfType(ConnectGuiMasterServer))
		this.enabled = false;
	
	// The username is passed in from another object in the login scene	
	usernameObject = GameObject.Find("EmptyUsernameObject");
	username = usernameObject.GetComponent(DontDestroyObject).username;

		// if username = "server", start the server
		if (username == "server") 
		{
			Debug.Log("running StartServer");
			Network.useNat = useNAT;
			Network.InitializeServer(32, listenPort);	//32 is the max # of connex we'll take

			// Notify our spawnpoint that the level and the network is ready
			var sp = GameObject.Find("SpawnPoint");
			sp.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
		}
	
		// otherwise connect to the server as a client
		else {
			Network.useNat = useNAT;
			Network.Connect(remoteIP, remotePort);
		}

}


// STILL HAVE A DISCONNECT BUTTON
function OnGUI() {
	GUILayout.Space(10);	

	GUILayout.BeginHorizontal(); //everything from here to EndHorizontal will be one after the other witih 10 spaces b/t
	GUILayout.Space(10);
	if (GUILayout.Button("Disconnect"))
	{
		Network.Disconnect(200); //200 is the timeout
	}
	GUILayout.FlexibleSpace();
	GUILayout.EndHorizontal();
}


// Automatically runs on the client when the client has successfully connected to a server
function OnConnectedToServer() {
	// Notify our objects that the level and the network is ready
	// Specifically, run the function OnNetworkLoadedLevel which is in SpawnPrefab.js
	// This fcn Instantiates a playerPrefab
	for (var go in FindObjectsOfType(GameObject))
		go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);		

}

// Automatically runs on the client when the client has disconnected from server
function OnDisconnectedFromServer () {
	Debug.Log("DISCONNECTING");
	if (this.enabled != false)
		Application.LoadLevel(Application.loadedLevel);
	else
		FindObjectOfType(NetworkLevelLoad).OnDisconnectedFromServer();
}

It looks to me like the OnDisconnectedFromServer() function is the source of your problem. It results in a dodgy loop which would then of course re-run the network.connect :slight_smile:

if (this.enabled != false) 
Application.LoadLevel(Application.loadedLevel); 
   else 
      FindObjectOfType(NetworkLevelLoad).OnDisconnectedFromServer();

This basically says ‘If this script is running, reload the current level’. You’ll probably want to change Application.LoadLevel(Application.loadedLevel); to Application.LoadLevel(“Name of login level”);