- Home /
OnConnectedToServer not working
I've created a server client model and successfully connect to a server of my program
However the OnConnectedToServer doesn't triggered even if the console already got the message from Network (I change the debug mode to "full")
here's the code, the OnConnectedToServer is written near the end.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public class serverlist : MonoBehaviour {
private string registeredgamehost = "XXXXXXXXXX1233543543";
private float refreshRequestLength = 3f;
private HostData[] hostData;
public GameObject samplebutton;
public Transform contentpanel;
public List<HostData> selectData = new List<HostData>();
public List<GameObject> buttons = new List<GameObject>();
[SerializeField] private InputField inputfield = null; // assign in the editor
// Use this for initialization
void Start () {
inputfield = gameObject.GetComponent<InputField> ();
Application.runInBackground = true;
}
// Update is called once per frame
void Update () {
}
public IEnumerator refreshserverlist(){
Debug.Log ("Refreshing...");
MasterServer.RequestHostList (registeredgamehost);
float start_time = Time.time;
float end_time = start_time + refreshRequestLength;
while (Time.time<end_time) {
hostData = MasterServer.PollHostList();
yield return new WaitForEndOfFrame();
}
Debug.Log ("number of hosts: "+hostData.Length);
listdesire ();
}
public void refresh(){
StartCoroutine ("refreshserverlist");
}
public void listdesire(){
Debug.Log("start extracting");
for (int i=0; i<buttons.Count; i++) {
Destroy(buttons[i]);
}
buttons.Clear();
selectData.Clear ();
string key = inputfield.text;
Debug.Log ("user input: " + key);
for (int i=0; i<hostData.Length; i++) {
if(hostData[i].gameName.Contains(key)&&hostData[i].connectedPlayers<2){
selectData.Add(hostData[i]);
}
}
Debug.Log ("the list contains"+selectData.Count+"elements");
foreach (HostData temp in selectData) {
GameObject newbutton = Instantiate(samplebutton) as GameObject;
SampleButton _button = newbutton.GetComponent<SampleButton>();
_button.nameLabel.text = temp.gameName;
_button.hostdata = temp;
_button.button.onClick.AddListener(() => connection_setup(temp));
newbutton.transform.SetParent(contentpanel);
buttons.Add(newbutton);
}
}
public void connection_setup(HostData data){
Network.Connect (data);
}
void OnConnectedToServer() {
Debug.Log("Connected to server");
}
void onFailedToConnect(){
Debug.Log ("Fail to connect");
}
}
anybody have an idea where might be wrong?
I made it work by just build the game to an android apk. but i suppose running in editor do not need to build. still a little confused though.
Answer by noahmilam · Sep 21, 2016 at 01:42 PM
from what I have read onConnectToServer() is part of the legacy pack. I would use public override void OnStartClient(){ Debug.Log ("on start client"); base.OnStartClient (); } https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613200437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Very Confused on Networking HLAPI Practices 1 Answer
How to watch in client applications what it is happening in the server one? 0 Answers
How do i control An Object between Server and Client 0 Answers
Client movements not registering on Server 1 Answer
Photon.Instantiate being called twice in OnloadedLevel and/or OnroomJoin 2 Answers