Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Third Party How can I check if I connected to the server?

Discussion in 'Multiplayer' started by Mamul, Oct 24, 2013.

  1. Mamul

    Mamul

    Joined:
    Sep 21, 2013
    Posts:
    11
    I'm sure I made a mistake in the code :

    Code (csharp):
    1.  
    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;
    }
    }
    }
    Code (csharp):
    1.  
    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 ?
     
  2. mrDave777

    mrDave777

    Joined:
    Aug 10, 2013
    Posts:
    8
    I think it is... if (PhotonNetwork.connected ) to check for a connection.

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

    Code (csharp):
    1. scrollPos = GUILayout.BeginScrollView(scrollPos);
    2.             foreach (RoomInfo game in PhotonNetwork.GetRoomList())
    3.             {
    4.                 GUILayout.BeginHorizontal();
    5.                 GUILayout.Label(game.name + " " + game.playerCount + "/" + game.maxPlayers);
    6.                 if (GUILayout.Button("JOIN"))
    7.                 {
    8.                     PhotonNetwork.JoinRoom(game.name);
    9.                     Destroy(this); 
    10.                 }
    11.                 GUILayout.EndHorizontal();
    12.             }
    13.             GUILayout.EndScrollView();
    You also should check out the photon vikings demo for a good example for a Main Menu GUI ;)
     
    aloften likes this.
  3. aloften

    aloften

    Joined:
    Aug 25, 2017
    Posts:
    18
    This is a dead post, but in case someone runs into it, like I did...

    Code (CSharp):
    1. using Photon.Pun;
    2. ...
    3. ...
    4. ...
    5. if (PhotonNetwork.IsConnected) {
    6.     JoinPlayerOverNetwork();
    7.  
    8. }
     
    AltiusGames and simonvutov1 like this.
  4. simonvutov1

    simonvutov1

    Joined:
    May 28, 2020
    Posts:
    4
    thanks this helped me!
     
  5. Gsinvention

    Gsinvention

    Joined:
    Sep 14, 2017
    Posts:
    1
    Thanks! such a simple answer, so difficult to find :')
     
  6. AltiusGames

    AltiusGames

    Joined:
    Oct 19, 2023
    Posts:
    1
    Thanks, very helpful