Starting FishNet's Connections
A guide on starting the FishNet server and client connections yourself.
This guide will show you how you can start the FishNet server and client yourself through code instead of relying only on the example NetworkHudCanvas or default autostart option.
Install FishNet
Before we begin, don't forget to install FishNet if you haven't already. You can follow this guide if needed: Installing Fish-Networking
Create a NetworkManager
Create a new game object in your scene and give it a nice name such as NetworkManager
.
Add the NetworkManager component to it.
Finally, add an ObserverManager component and in the Default Conditions field, add a new element and select the SceneCondition.

Setup Basic UI
To get started, let's add a basic user interface which we can use to start the FishNet server, client, and allow the user to enter an IP Address to connect to.
Add three buttons to your game, label them Start Host, Start Server, and Start Client. Now add an input field for the address the client will attempt to connect to.

Create a ConnectionManager Script
Let's now create the following script and add it to our game object.
using FishNet.Managing;
using UnityEngine;
public class ConnectionManager : MonoBehaviour
{
[SerializeField] NetworkManager networkManager;
// A host is simply a server and a client, so start them both.
public void StartHost()
{
StartServer();
StartClient();
}
// The server can be started directly from the ServerManager or Transport
public void StartServer()
{
networkManager.ServerManager.StartConnection();
}
// The client can be started directly from the ClientManager or Transport
public void StartClient()
{
networkManager.ClientManager.StartConnection();
}
// This is set on the Transport to indicate where the client should connect.
public void SetIPAddress(string text)
{
networkManager.TransportManager.Transport.SetClientAddress(text);
}
}
The code show here is very simple, but one thing to observe is that the script does not inherit from NetworkBehaviour. This is because this script will be active before the network is started and after it has stopped.
Assign the NetworkManager
Our ConnectionManager needs a reference to the NetworkManager component, assign that in the editor now.

Hook-Up the UI to the Code
Now select your UI components and attach their OnClick events to the relevant methods in our ConnectionManager.

For the IP Address Input Field, you can hook up the On Value Changed event to the ConnectionManager.SetIPAddress
method we created.

Test it out!
With that all done you should be able to run the game and use the UI we created to start the FishNet server and/or client.

You should try extending these buttons to also include stopping the server and clients, you can do those actions directly from the ServerManager and ClientManager components as well.
Last updated