Search Unity

Question Netcode and Relay working fine in the Editor but not in the built game.

Discussion in 'Netcode for GameObjects' started by KristenBoyGames, Apr 16, 2022.

  1. KristenBoyGames

    KristenBoyGames

    Joined:
    May 23, 2021
    Posts:
    38
    The Netcode and Relay work with no errors in the unity editor but in a built game it doesn't work at all.
    here is my RelayManager script, im pretty sure the issue is in this script.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using Balls.Singleton;
    4. using System.Threading.Tasks;
    5. using Unity.Netcode;
    6. using Unity.Services.Authentication;
    7. using Unity.Services.Core;
    8. using Unity.Services.Core.Environments;
    9. using Unity.Services.Relay;
    10. using Unity.Services.Relay.Models;
    11. using Unity.Netcode.Transports.UTP;
    12. using TMPro;
    13.  
    14.  
    15.  
    16.  
    17. public class RelayManager : Singleton<RelayManager>
    18. {
    19.     [SerializeField]
    20.     private string environment = "production";
    21.  
    22.     [SerializeField]
    23.     private int maxNumberOfConnections = 10;
    24.  
    25.     public bool IsRelayEnabled => Transport != null && Transport.Protocol == UnityTransport.ProtocolType.RelayUnityTransport;
    26.  
    27.     public UnityTransport Transport => NetworkManager.Singleton.gameObject.GetComponent<UnityTransport>();
    28.  
    29.     public TMP_Text gamepin;
    30.     private string gamepintext;
    31.    
    32.  
    33.  
    34.     public async Task<RelayHostData> SetupRelay()
    35.     {
    36.         InitializationOptions options = new InitializationOptions().SetEnvironmentName(environment);
    37.  
    38.         await UnityServices.InitializeAsync(options);
    39.  
    40.         if (!AuthenticationService.Instance.IsSignedIn)
    41.         {
    42.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
    43.             debugscript.signedIn();
    44.         }
    45.  
    46.         Allocation allocation = await Relay.Instance.CreateAllocationAsync(maxNumberOfConnections);
    47.  
    48.         RelayHostData relayHostData = new RelayHostData
    49.         {
    50.             Key = allocation.Key,
    51.             Port = (ushort)allocation.RelayServer.Port,
    52.             AllocationID = allocation.AllocationId,
    53.             AllocationIDBytes = allocation.AllocationIdBytes,
    54.             IPv4Address = allocation.RelayServer.IpV4,
    55.             ConnectionData = allocation.ConnectionData
    56.         };
    57.  
    58.         relayHostData.JoinCode = await Relay.Instance.GetJoinCodeAsync(relayHostData.AllocationID);
    59.  
    60.         Transport.SetRelayServerData(relayHostData.IPv4Address, relayHostData.Port, relayHostData.AllocationIDBytes,
    61.                 relayHostData.Key, relayHostData.ConnectionData);
    62.  
    63.         gamepintext = relayHostData.JoinCode.ToString();
    64.         gamepin.SetText(relayHostData.JoinCode);
    65.         debugscript.hoststarted();
    66.         return relayHostData;
    67.        
    68.     }
    69.    /* private void FixedUpdate()
    70.     {
    71.         if (gamepin.text != gamepintext)
    72.             gamepin.SetText(gamepintext);
    73.  
    74.     }*/
    75.     public async Task<RelayJoinData> JoinRelay(string joinCode)
    76.     {
    77.         InitializationOptions options = new InitializationOptions()
    78.             .SetEnvironmentName(environment);
    79.  
    80.         await UnityServices.InitializeAsync(options);
    81.  
    82.         if (!AuthenticationService.Instance.IsSignedIn)
    83.         {
    84.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
    85.         }
    86.  
    87.         JoinAllocation allocation = await Relay.Instance.JoinAllocationAsync(joinCode);
    88.      
    89.         RelayJoinData relayJoinData = new RelayJoinData
    90.         {
    91.             Key = allocation.Key,
    92.             Port = (ushort)allocation.RelayServer.Port,
    93.             AllocationID = allocation.AllocationId,
    94.             AllocationIDBytes = allocation.AllocationIdBytes,
    95.             ConnectionData = allocation.ConnectionData,
    96.             HostConnectionData = allocation.HostConnectionData,
    97.             IPv4Address = allocation.RelayServer.IpV4,
    98.             JoinCode = joinCode
    99.         };
    100.  
    101.         Transport.SetRelayServerData(relayJoinData.IPv4Address, relayJoinData.Port, relayJoinData.AllocationIDBytes,
    102.             relayJoinData.Key, relayJoinData.ConnectionData, relayJoinData.HostConnectionData);
    103.  
    104.        
    105.         return relayJoinData;
    106.        
    107.     }
    108.    
    109. }
    110.