Hello,
When setting up Unity Netcode and Relay I was testing it on the same machine by running multiple instances, which is entirely functional. Where I started running into issues is when connecting two builds on two different computers. Instead of joining the same session they both became the hosts of two different lobbies. Even when trying to type in the join code manually they won’t connect to each other.
Here’s my code:
public class TestingNetcodeUI : MonoBehaviour
{
string joinCode;
private async void Awake()
{
await UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
StartGame();
}
private async void StartGame()
{
QueryResponse queryResponse = await UpdateLobbyList();
if (queryResponse.Results.Count >= 1) //if there is an active lobby, join it
{
Debug.Log("client");
Debug.Log(joinCode + "join code");
Debug.Log(queryResponse.Results[0].Name + "lobby code");
JoinRelay(queryResponse.Results[0].Name); //gets the lobby it found and uses its name to join the same relay the host did
}
else //otherwise, make one
{
Debug.Log("host");
joinCode = await CreateRelay(); //creates relay and sets relay join code to this string
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(joinCode, 8); //makes relay join code the name of the lobby it just created
Debug.Log(joinCode + "join code");
}
}
private async Task<QueryResponse> UpdateLobbyList()
{
QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync();
return queryResponse;
}
private async Task<string> CreateRelay()
{
try
{
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(10);
string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartHost();
return joinCode;
}
catch (RelayServiceException e)
{
Debug.Log(e);
return null;
}
}
private async void JoinRelay(string joinCode)
{
try
{
Debug.Log("Joining Relay with" + joinCode);
JoinAllocation joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
RelayServerData relayServerData = new RelayServerData(joinAllocation, "dtls");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartClient();
}
catch (RelayServiceException e)
{
Debug.Log(e);
StartGame();
}
}
}
Any help would be appreciated.