- Home /
UnityNetworking fails to list matches..
Hey Unity Devs! So, I am making a Quizz multiplayer game in which 2 players select a category and they shall start a match, the problem is that I couldn't do that using unity networking. here is my code, what I am trying to achieve is:
I call the FindMatch() Function when ever the player selects a category.
try to find a match whenever a player selects a category looking for that categoryRoom. if the room wasn't there, then the player starts a room for 15 seconds then if no one showed up. they will play against bots. if hey found a game that is not full they should join. in my code, it just creates the room on each client..
here is my code:
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using System.Collections.Generic;
public class MatchManager : NetworkBehaviour{
public static PlayerScript player;
public static MatchManager Instance;
public MatchInfo currentMatch;
public static int status = 0;
public string categoryToFind;
private void Awake()
{
Instance = this;
NetworkManager.singleton.StartMatchMaker();
}
public void CreateInternetMatch(string matchName)
{
NetworkManager.singleton.matchMaker.CreateMatch(matchName, 2, true, "", "", "", 0, 0, OnInternetMatchCreate);
}
private void OnInternetMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
Debug.Log("Create match succeeded");
MatchInfo hostInfo = matchInfo;
NetworkServer.Listen(hostInfo, 9000);
NetworkManager.singleton.StartHost(hostInfo);
currentMatch = matchInfo;
}
else
{
Debug.LogError("Create match failed");
}
}
public void FindInternetMatch(string matchName)
{
categoryToFind = matchName;
NetworkManager.singleton.matchMaker.ListMatches(0, 10, matchName, true, 0, 0, OnInternetMatchList);
}
private void OnInternetMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matches)
{
if (success)
{
if (matches.Count != 0)
{
foreach (MatchInfoSnapshot match in matches)
{
Debug.Log(match.currentSize);
if (match.currentSize < match.maxSize)
{
Debug.Log(match.name + " is available");
NetworkManager.singleton.matchMaker.JoinMatch(match.networkId, "", "", "", 0, 0, OnJoinInternetMatch);
return;
}
}
}
else
{
CreateInternetMatch(categoryToFind);
}
}
else
{
Debug.LogError("Couldn't connect to match maker");
}
}
//this method is called when your request to join a match is returned
private void OnJoinInternetMatch(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
Debug.Log("Able to join a match");
MatchInfo hostInfo = matchInfo;
NetworkManager.singleton.StartClient(hostInfo);
currentMatch = matchInfo;
}
else
{
Debug.LogError("Join match failed");
}
}
int counter = 0;
public override void OnStartClient()
{
counter++;
base.OnStartClient();
Debug.Log("Client " + counter + " Started");
if(counter == 2)
{
CmdStartMatch();
}
}
public override void OnStartServer()
{
base.OnStartServer();
Debug.Log("Server Started");
}
public override void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();
Debug.Log("LocalPlayerConnected");
}
public void StopConnection()
{
NetworkManager.singleton.matchMaker.DestroyMatch(currentMatch.networkId, 0, OnMatchDestroy);
}
private void OnMatchDestroy(bool success, string extdInfo)
{
if (success)
Debug.Log("Match Destroyed");
else
Debug.LogError("Match not destroyed");
}
[Command]
public void CmdStartMatch()
{
RpcStartMatch();
Debug.Log("StartMatch_ServerCommand");
}
[ClientRpc]
public void RpcStartMatch()
{
Debug.Log("Start Match!");
QuizzGame.isBot = false;
UiManager.Instance.ShowQuizzScreenOnly();
CategoryUI.joinedAmatch = true;
QuizzGame.Instance.quizzStarted = true;
}
}
Your answer
Follow this Question
Related Questions
Getting error when making a chat system 0 Answers
Question on Development Priorities. 0 Answers
NetworkManager.OnServerDisconnect not being called 2 Answers
Unity's LLAPI for Raspberry Pi Server? 0 Answers
Random Map doesn't want spawn in Clients 0 Answers