How to Disconnect a client from the server properly using unity Netcode for GameObjects ?
I am new to Unity's Netcode and I have implemented the client disconnect and it works perfectly for the first disconnect but the after that (during reconnect) I get these errors -:
I am Instantiating the two main Networked GameObjects
, one being the NetworkManager
itself and the other being ServerGameNetPortal
from the script for only once i.e. only at the start of the game and then have applied the singleton so these objects do not get destroyed upon scene loading.
Here is the client disconnect code -: (This is from another scene => lobby)
public void OnClientDisconnect(ulong clientId)
{
if (!IsServer) { return; }
else
{
ClientData.Remove(clientId);
NetworkManager.Singleton.DisconnectClient(clientId);
}
}
And this is the Method which gets fired upon client disconnection -:
private void HandleClientDisconnected(ulong _clientId)
{
if (IsServer) { return; }
else
{
NetworkManager.Singleton.Shutdown(true);
SceneManager.LoadScene("StartScene", LoadSceneMode.Single);
}
}
In here I have tried removing the NetworkManager.Singleton.Shutdown()
but still it does'nt makes any difference. (This was for the client side).
Now for the server side =:
everything works fine for the first disconnect but after that ServerGameNetPortal
GameObject gets automatically destroyed. which should'nt be happening as due to the singleton pattern.
Please help Been stuck here for almost 2 days.
Your answer
Follow this Question
Related Questions
[Hololens] Connecting using Unity's UNET doesn't work after the first time connecting 1 Answer
Fixedjoints Not Updating Position In UNet 0 Answers
Network Transport Layer API does not work with iOS to PC???? 0 Answers
Null Reference Exception when finding GameObject on Networked Game 1 Answer
I am using unity 2020-3-14f1 and having a bit trouble with low fps for a very low end game ! 1 Answer