- Home /
PHOTON "Count Of players" not working for Client 1?
Hey gang, I have a small problem with my game, everything seems to work find except with Client 1. the first client to load stays on "Waiting for Players to Join" when the Second Client connects Client to Proceeds to show the "Launch" Button, Client 1 should also show the "LAUNCH" button when their are 2 or more Players in "Count of Players". so plays can only start the game when both players are connected have I missed a step:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
public class ConnectOnline : Photon.MonoBehaviour {
public GameObject EarthMap;
public GameObject MarsMap;
public GameObject LaunchButton;
public float MapSelect;
public virtual void Start()
{
PhotonNetwork.autoJoinLobby = true; // we join randomly. always. no need to join a lobby to get the list of rooms.
PhotonNetwork.ConnectUsingSettings("0.1");
}
public void OnConnectedToMaster()
{
Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
PhotonNetwork.JoinRandomRoom();
}
public void OnJoinedLobby()
{
Debug.Log("OnJoinedLobby(). This client is connected and does get a room-list, which gets stored as PhotonNetwork.GetRoomList(). This script now calls: PhotonNetwork.JoinRandomRoom();");
PhotonNetwork.JoinRandomRoom();
RoomOptions roomOptions = new RoomOptions () { isVisible = true, maxPlayers = 2 };
PhotonNetwork.JoinOrCreateRoom ("roomName", roomOptions, TypedLobby.Default);
}
public void OnPhotonRandomJoinFailed()
{
RoomOptions roomOptions = new RoomOptions () { isVisible = true, maxPlayers = 2 };
Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
PhotonNetwork.JoinOrCreateRoom ("roomName", roomOptions, TypedLobby.Default);
}
public void Update() {
if(PhotonNetwork.countOfPlayers == 2){
LaunchButton.SetActive (true);
}
if (PhotonNetwork.isMasterClient == true && PhotonNetwork.countOfPlayers == 2) {
MapSelect = 0;
MapSelect = Random.Range (0, 6);
if (MapSelect > 0 && MapSelect < 3.1) {
EarthMap.SetActive (true);
photonView.RPC ("FireOtherTeam1", PhotonTargets.OthersBuffered);
}
if (MapSelect > 3.1 && MapSelect < 6) {
MarsMap.SetActive (true);
photonView.RPC ("FireOtherTeam2", PhotonTargets.OthersBuffered);
}
}
}
[PunRPC]
void FireOtherTeam2 (){
EarthMap.SetActive (true);
}
[PunRPC]
void FireOtherTeam1 (){
MarsMap.SetActive (true);
}
}
][1]
Answer by ChristianSimon · Nov 28, 2017 at 09:58 AM
Hi,
there is a problem with your OnJoinedLobby callback function. In this function you are calling JoinRandomRoom and JoinOrCreateRoom which doesn't work the way you want. Try using either one of these. You can for example use JoinRandomRoom and additionally implement the OnPhotonRandomJoinFailed(object[] codeAndMsg) { // codeAndMsg[0] is short ErrorCode. codeAndMsg[1] is string debug msg. }
callback to get notified if there is no random room available. In this case you can also use this callback to call JoinOrCreateRoom or just CreateRoom in order to create and join a new room.
To get the number of players in your room you can use PhotonNetwork.room.PlayerCount
instead of countOfPlayers. Before checking PhotonNetwork.room.PlayerCount please make sure that the client actually is inside a room by using the PhotonNetwork.inRoom
condition.
Your answer
Follow this Question
Related Questions
UNet, how to create Internet game (like via PhotonNetwork) 0 Answers
Why does Photon find a room so quick! 1 Answer
photon marco polo tutorial issue with "peercreated" 1 Answer
Need help with joining a room in photon? 1 Answer
GUI issues 1 Answer