How can I check if I connected to the server?

I'm sure I made a mistake in the code :

``using UnityEngine;
using System.Collections;

public class MultyPlayer : MonoBehaviour {

private float buttonXpos=Screen.width/2;
private float buttonYpos=Screen.height/2;
private bool joinedRoom;
private bool createdRoom;
private int numberRooms;

// Use this for initialization
void Start () {
PhotonNetwork.ConnectUsingSettings("v1.0");
}

// Update is called once per frame
void Update () {

}
void onJoinedLobby(){
numberRooms=PhotonNetwork.countOfPlayersOnMaster;
if(createdRoom==true){
PhotonNetwork.CreateRoom(null ,false,false ,2);
PhotonNetwork.LoadLevel("Scene");
}

if(joinedRoom == true){
PhotonNetwork.JoinRandomRoom();
PhotonNetwork.Instantiate("Sphere_prefab", new Vector3(0,0,0),
Quaternion.identity, 0);
}

}
//User Interface
void OnGUI() {
if(GUI.Button(new Rect (buttonXpos , buttonYpos , 160 , 40) , "Connect to a room " + numberRooms)){
joinedRoom=true;

}
if(GUI.Button (new Rect (buttonXpos , buttonYpos+40 , 160 , 40), "Create Game" )){
createdRoom=true;
}
}
}


I have two buttons one to create a game and one to join.In the Join game button it should display how many players are connected to the master server but it keeps showing me 0 even when I'm there.So I don't know if I connected to the server , it seems something is wrong.Any suggestions ?

I think it is... if (PhotonNetwork.connected ) to check for a connection.

To see your rooms players you can use something like ....

scrollPos = GUILayout.BeginScrollView(scrollPos);
            foreach (RoomInfo game in PhotonNetwork.GetRoomList())
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(game.name + " " + game.playerCount + "/" + game.maxPlayers);
                if (GUILayout.Button("JOIN"))
                {
                    PhotonNetwork.JoinRoom(game.name);
                    Destroy(this);  
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();

You also should check out the photon vikings demo for a good example for a Main Menu GUI ;)

1 Like

This is a dead post, but in case someone runs into it, like I did...

using Photon.Pun;
...
...
...
if (PhotonNetwork.IsConnected) {
    JoinPlayerOverNetwork();

}
2 Likes


thanks this helped me!

Thanks! such a simple answer, so difficult to find :')


Thanks, very helpful