I have a relay. With a click on a “Host” button in my UI, this code is executed:
public async void StartHost(){
Debug.Log("En StartHost, de MainMenu");
HostSingleton hostSingleton = HostSingleton.Instance;
HostGameManager hostGameManager = hostSingleton.HostGameManager;
if(hostGameManager !=null){
Debug.Log("We get hostGameManager");
}else{
Debug.Log("hostGameManager is null");
hostSingleton.CreateHost();
hostGameManager = hostSingleton.HostGameManager;
}
await hostGameManager.StartHostAsync();
//await HostSingleton.Instance.HostGameManager.StartHostAsync();
}
HostSingleton class:
public class HostSingleton : MonoBehaviour
{
public static HostSingleton instance;
public HostGameManager HostGameManager {get; private set;}
public static HostSingleton Instance
{
get
{
if(instance != null){
Debug.Log("Ya tenemos la instancia, la devolvemos");
return instance;
}
instance = FindAnyObjectByType<HostSingleton>();
if(instance == null){
Debug.LogWarning("HostSingleton es nulo");
return null;
}
return instance;
}
}
private void Start()
{
DontDestroyOnLoad(gameObject);
}
public void CreateHost(){
Debug.Log("CreateHost");
HostGameManager = new HostGameManager();
if(HostGameManager == null){
Debug.Log("HostGameManager is null");
}else{
Debug.Log("We got HostGameManager");
}
}
}
HostGameManager:
public class HostGameManager
{
private const int MaxConnections = 20;
private const string GameSceneName = "Game";
private Allocation allocation;
private string joinCode;
public async Task StartHostAsync(){
Debug.Log("En StartHostAsync");
try
{
allocation = await RelayService.Instance.CreateAllocationAsync(MaxConnections);
}
catch (Exception e)
{
Debug.Log(e);
return;
}
try
{
joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
Debug.Log(joinCode);
}
catch (Exception e)
{
Debug.Log(e);
return;
}
UnityTransport transport = NetworkManager.Singleton.GetComponent<UnityTransport>();
RelayServerData relayServerData = new RelayServerData(
allocation.RelayServer.IpV4,
(ushort)allocation.RelayServer.Port,
allocation.AllocationIdBytes,
allocation.ConnectionData,
allocation.ConnectionData,
allocation.Key,
/* isSecure= */ true);
transport.SetRelayServerData(relayServerData);
//Arrancamos el Host
NetworkManager.Singleton.StartHost();
NetworkManager.Singleton.SceneManager.LoadScene(GameSceneName, UnityEngine.SceneManagement.LoadSceneMode.Single);
}
}
And, an ApplicationController class:
public class ApplicationController : MonoBehaviour
{
[SerializeField] ClientSingleton clientPrefab;
[SerializeField] HostSingleton hostPrefab;
private async Task Start()
{
Debug.Log("En el Start de ApplicationController");
DontDestroyOnLoad(gameObject);
await LaunchInMode(SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Null);
}
private async Task LaunchInMode(bool isDedicatedServer){
if(isDedicatedServer){
}else{
Debug.Log("LaunchInMode");
HostSingleton hostSingleton = Instantiate(hostPrefab);
hostPrefab.CreateHost();
ClientSingleton clientSingleton = Instantiate(clientPrefab);
bool authenticated = await clientSingleton.CreateClient();
if(authenticated){
//Al main menu
clientSingleton.ClientGameManager.GoToMenu();
}
}
}
}
This way, the game works when I click Host, I am able to play the game…for a minute EXACT. When that minute has passed, the host falls, and I receive some error messages:
What is going on?? How can I fix that?
Thank you.
