Hi,
I am currently working on chess-like 1v1 game, and I am having bug that I am unable to solve. For networking I am using Relay, and on the high level the networking looks something like that:
Joining player inputs code, when host detects 2 data transmitters, it sends to the other client important data, and then they both communicate using RPC(the data that is sent in the beginning is too sent using this way). And when the player quits the game, i tam using simply this line to disconnect:
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsClient)
{
NetworkManager.Singleton.Shutdown();
}
But after disconnecting this way, when host tries to host another game i get this error:
NullReferenceException: Object reference not set to an instance of an object
Unity.Netcode.NetworkBehaviour.__endSendClientRpc (Unity.Netcode.FastBufferWriter& bufferWriter, System.UInt32 rpcMethodId, Unity.Netcode.ClientRpcParams clientRpcParams, Unity.Netcode.RpcDelivery rpcDelivery) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.7.1/Runtime/Core/NetworkBehaviour.cs:210)
DataTransmitter.SendColorToClientRpc (System.Boolean color) (at Assets/Scripts/Networking/DataTransmitter.cs:165)
DataTransmitter.SendColor () (at Assets/Scripts/Networking/DataTransmitter.cs:156)
HostCanvas.SetColors () (at Assets/Scripts/Canvases/HostCanvas.cs:134)
HostCanvas.OnEnable () (at Assets/Scripts/Canvases/HostCanvas.cs:69)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <f7237cf7abef49bfbb552d7eb076e422>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <f7237cf7abef49bfbb552d7eb076e422>:0)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <f7237cf7abef49bfbb552d7eb076e422>:0)
this error occures here, and it occurs only on the host side, and client never gets the message:
public void SendColor(){
if (NetworkManager == null) {
Debug.LogError("NetworkManager.Singleton is null!1");
return;
}
if(NetworkManager.Singleton.IsHost){
SendColorToClientRpc(!gameManager.color);
}
}
[ClientRpc]
private void SendColorToClientRpc(bool color){
Debug.Log("RECEIVING COLORRRRRRRRRRRRRR"); // The error is in this line
if (!NetworkManager.Singleton.IsHost) {
gameManager.SetColor(color);
}
}
I am not sure if it helps, but two observations that I made are that:
- When the user who was prieviously joining one, hosted game, everything would work correctly, but after dissconnecting again, it would start being totally unpredictable and sometimes it would work sometimes it wouldn’t
- Every time when I am trying to leave a match, and host another one, the client iD for host is always 0, and for the client it is always 1 more than before, if connection was cut, shouldn’t it be always this always be 0 and 1?
I have already seen this post Issue with Lobby - Unable to Create New Lobby After Playing a Match but I don’t use anywhere events in networking, so it didn’t help me in any way.
I would appreciate any insights or suggestions on how to resolve this issue. If you need any additional information or code snippets, please let me know, and I’ll be happy to provide them.
Thank you in advance
Mawerty