Networking UNet: Why can we have multiple NetworkClient and how NetworkBehaviour knows which one to use?


The title says it all but I’ll elaborate.
I’m learning to use the UNet network and some of parts the design of creating the connection between client and server confuse me.

You see, when you start a server in UNet you just use the static class NetworkServer and call Listen(). Everything is static so there is no ambiguity there.

But when you want to connect a client to a server you first have to create a new (non-static) NetworkClient object and then connect this NetworkClient instance to the server using Connect().

  • Why do we have to instantiate a NetworkClient at all? This suggest that we might want to have multiple NetworkClient instances in the same game process. If so, what is the use of it (connecting to multiple servers?)?

  • If we really can have multiple
    connections how does this play with
    NetworkBehaviour synchronisation? How
    do NetworkBehaviours know on which
    connection to serialize to?

  • Since NetworkClients are instanciated
    do I need to keep a reference of
    their instance if I want to keep the
    connection? Otherwise my impression
    would be that the connection will be

  • Regarding my previous question, I’ve found the NetworkClient.allClients
    array variable, a static array listing all NetworkClient instances. Would setting this variable to null then trigger a garbage-collect of the client connections?

I just can’t get my head around why would a static class suffice for the Server but not for the Client.

For whoever takes the time to answer: Thank you very much! :slight_smile:

Re: Multiple NetworkClients: Say you’re making a turn-based game. You want to have online play, single-player and hot-seat (taking turns on the same device).

For the 1st option, you’ll only need one client, (and a server if you’re the host). For the 2nd and 3rd option, you’re going to need multiple clients on the same process. You could just create a whole different set of classes for single-player and hot-seat play, but this way you don’t have to, you can just re-use the architecture you used for online play, but just change the number of clients you spawn.

Re: Garbage collection. Yes, I believe you are correct.

Hopefully, someone can answer your 2nd bullet point question.