Hey there, thanks for helping me out!
I have written a script that extends from NetworkManager that should look for matches and if it doesn’t find any, starts a match of it’s own.
The script detects matches, joins and creates, but when a client joins the server gets an error.
The Error I get when connecting with a client is:
“There is already a player at that playerControllerId for this connections.
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()”
I have checked in Debug mode on the server and both playerobjects have networkID 0.
Does anybody know what to do about this?
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
public class Lobby : NetworkManager {
public int maxJoinAttempts = 5;
public Canvas lobbyUi;
bool isReady;
public void ToggleReady () //this is called by a UI toggle
{
isReady = !isReady;
if (isReady)
{
StartMatchMaker();
StartCoroutine(FindMatch());
}
else
{
StopMatchMaker();
StopCoroutine(FindMatch());
}
}
IEnumerator FindMatch ()
{
for (int a = 0; a < maxJoinAttempts; a++)
{
//try to connect to a match
matchMaker.ListMatches(0, 100, "", true, 0, 0, OnMatchList);
yield return new WaitForSeconds(1);
}
OnFindMatchFailed();
}
public override void OnMatchList (bool succes, string extendedInfo, List<MatchInfoSnapshot> matchList)
{
matches = matchList;
if (succes)
{
if (matches.Count != 0)
{
Debug.Log("Matches found");
OnMatchFound(matches[0]);
}
else
{
Debug.Log("No matches found");
}
}
else
{
Debug.Log("Could not connect to matchmaker");
}
}
void OnMatchFound (MatchInfoSnapshot match)
{
Debug.Log("Connecting to match");
matchMaker.JoinMatch(match.networkId, "", "", "", 0, 0, OnMatchJoined);
}
void OnFindMatchFailed ()
{
CreateMatch ();
}
void CreateMatch ()
{
matchMaker.CreateMatch("PlayerMatch", matchSize, true, "", "", "", 0, 0, OnMatchCreate);
}
}
OK, so the problem turned out to be my coroutine “FindMatch” not stopping after joining.
It tried to connect to the same server again and again after already being connected.