- Home /
How to connect to our own dedicated server using photon networking in unity?(Self-hosted)
In below script am trying to connect my game with my own server. But every time I get an error like - (JoinRandomRoom failed. Client is on MasterServer (must be Master Server for matchmaking) but not ready for operations (State: ConnectingToMasterServer). Wait for callback: OnJoinedLobby or OnConnectedToMaster.)
please check my below script and check what is wrong.
[SerializeField] private GameObject FindOppenentTxt = null; [SerializeField] private GameObject waitingStatusPanel = null; [SerializeField] private Text waitingStatusText = null; private bool isConnnecting = false; [SerializeField]
private const string GameVersion = "1.1";
private const int maxPlayer = 2;
private string masterServerAddress = "**********";
private int port = 7777;
private string appID = "***********7d645e794534";
private void Awake()
{
PhotonNetwork.AutomaticallySyncScene = true;
}
private void Start()
{
PhotonNetwork.SendRate = 20;
PhotonNetwork.SerializationRate = 5;
PhotonNetwork.ConnectToMaster(masterServerAddress, port, appID);
//PhotonNetwork.ConnectUsingSettings();
}
public void FindOpponent()
{
isConnnecting = true;
FindOppenentTxt.SetActive(false);
waitingStatusPanel.SetActive(true);
waitingStatusText.text = "Searching...";
if(PhotonNetwork.IsConnected)
{
PhotonNetwork.JoinRandomRoom();
Debug.Log("findingRoom");
}
else
{
//PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = maxPlayer });
Debug.Log("creatingRoom");
PhotonNetwork.GameVersion = GameVersion;
//PhotonNetwork.ConnectUsingSettings();
PhotonNetwork.ConnectToMaster(masterServerAddress, port, appID);
Debug.Log(PhotonNetwork.CurrentRoom);
}
}
public override void OnConnectedToMaster() { if(isConnnecting) { PhotonNetwork.JoinRandomRoom(); Debug.Log("Server IP: " + PhotonNetwork.ServerAddress); } }
public override void OnDisconnected(DisconnectCause cause)
{
waitingStatusPanel.SetActive(false);
FindOppenentTxt.SetActive(true);
Debug.Log("disconnected");
}
public override void OnJoinRandomFailed(short returnCode, string message)
{
Debug.Log("There are no clients");
PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = maxPlayer });
}
public override void OnJoinedRoom()
{
Debug.Log("Joined a room ssuccessfully");
int playerCount = PhotonNetwork.CurrentRoom.PlayerCount;
if(playerCount != maxPlayer)
{
waitingStatusText.text = "waiting for an opponent";
}
else
{
waitingStatusText.text = "Opponent Found...please wait..";
}
}
public override void OnPlayerEnteredRoom(Player newPlayer)
{
if (PhotonNetwork.CurrentRoom.PlayerCount == maxPlayer)
{
PhotonNetwork.CurrentRoom.IsOpen = false;
waitingStatusText.text = "opponent in room";
PhotonNetwork.LoadLevel("MainScene");
}
}
}