# Starting FishNet's Connections

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](https://fish-networking.gitbook.io/docs/fishnet-building-blocks/prefabs/networkhudcanvas "mention") or default [autostart](https://fish-networking.gitbook.io/docs/fishnet-building-blocks/components/managers/server-manager#start-on-headless) option.

{% stepper %}
{% step %}

#### 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](https://fish-networking.gitbook.io/docs/tutorials/getting-started/installing-fish-networking "mention")
{% endstep %}

{% step %}

#### Create a NetworkManager

Create a new game object in your scene and give it a nice name such as `NetworkManager`.

Add the [NetworkManager component](https://fish-networking.gitbook.io/docs/fishnet-building-blocks/components/managers/network-manager) to it.

Finally, add an [ObserverManager component](https://fish-networking.gitbook.io/docs/fishnet-building-blocks/components/managers/observermanager) and in the **Default Conditions** field, add a new element and select the [SceneCondition](https://fish-networking.gitbook.io/docs/fishnet-building-blocks/scriptableobjects/observerconditions/scenecondition).

<figure><img src="https://1328095063-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MheH2hMo3djr9VSyxTE%2Fuploads%2Fgit-blob-21c1b03c75d49eb6ebc9e11f79b5c5fd5302c7b6%2Fstarting-connections-network-manager.png?alt=media" alt=""><figcaption><p>The NetworkManager components</p></figcaption></figure>
{% endstep %}

{% step %}

#### 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.

<figure><img src="https://1328095063-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MheH2hMo3djr9VSyxTE%2Fuploads%2Fgit-blob-818479237045ada7472cad04ad5db3134362134e%2Fstarting-connections-scene.png?alt=media" alt=""><figcaption><p>The scene setup</p></figcaption></figure>

{% hint style="info" %}
You can download the graphics used above here:

<a href="https://github.com/maxkratt/fish-networking-starting-connections/releases/download/v1.0-starting-connections-guide/Graphics.zip" class="button primary" data-icon="down-to-line">Download</a>
{% endhint %}
{% endstep %}

{% step %}

#### Create a ConnectionManager script

Let's now create the following script and add it to our [**Canvas**](#user-content-fn-1)[^1] game object.

{% code title="ConnectionManager.cs" lineNumbers="true" %}

```csharp
using FishNet.Managing;
using UnityEngine;

public class ConnectionManager : MonoBehaviour
{
    [SerializeField] private NetworkManager _networkManager;

    public void StartHost()
    {
        StartServer();
        StartClient();
    }

    public void StartServer()
    {
        _networkManager.ServerManager.StartConnection();
    }

    public void StartClient()
    {
        _networkManager.ClientManager.StartConnection();
    }

    public void SetIPAddress(string text)
    {
        _networkManager.TransportManager.Transport.SetClientAddress(text);
    }
}
```

{% endcode %}

The code show here is very simple, but one thing to observe is that the script does not inherit from [NetworkBehaviour](https://fish-networking.gitbook.io/docs/fishnet-building-blocks/components/network-behaviour-components). This is because this script will be active before the network is started and after it has stopped.
{% endstep %}

{% step %}

#### Assign the NetworkManager

Our **ConnectionManager** needs a reference to the NetworkManager component, assign that in the editor now.

<figure><img src="https://1328095063-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MheH2hMo3djr9VSyxTE%2Fuploads%2Fgit-blob-8e2a92e5765e6d5f678899874bdfee44c187b813%2Fstarting-connections-canvas-connection-manager.png?alt=media" alt=""><figcaption><p>ConnectionManager assigned a NetworkManager</p></figcaption></figure>
{% endstep %}

{% step %}

#### Hook-up the UI to the code

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

<figure><img src="https://1328095063-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MheH2hMo3djr9VSyxTE%2Fuploads%2Fgit-blob-e8a467d6d8027ed2331f8096ef8923ea97f730a0%2Fstarting-connections-host-button.png?alt=media" alt=""><figcaption><p>Hooking up the host button</p></figcaption></figure>

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

<figure><img src="https://1328095063-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MheH2hMo3djr9VSyxTE%2Fuploads%2Fgit-blob-4becd79555efd070f387f0c534b672cf53607085%2Fstarting-connections-input-field.png?alt=media" alt=""><figcaption><p>Setting the IP address input field</p></figcaption></figure>
{% endstep %}

{% step %}

#### 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.

<figure><img src="https://1328095063-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MheH2hMo3djr9VSyxTE%2Fuploads%2Fgit-blob-879fbb0a2857e7a08f289517bd696689949f4954%2Fstarting-connections-example.gif?alt=media" alt=""><figcaption></figcaption></figure>

You should try extending these buttons to also include stopping the server and clients, you can do those actions directly from the [ServerManager](https://github.com/FirstGearGames/FishNet-Documentation/blob/main/guides/fishnet-building-blocks/components/managers/server-manager.md) and [ClientManager](https://github.com/FirstGearGames/FishNet-Documentation/blob/main/guides/fishnet-building-blocks/components/managers/clientmanager.md) components as well.
{% endstep %}
{% endstepper %}

{% hint style="info" %}
Download the project files with these completed steps here, or explore the repository:

<a href="https://github.com/maxkratt/fish-networking-starting-connections/releases/download/v1.2-starting-connections-guide/starting-connections.unitypackage" class="button primary" data-icon="down-to-line">Source Files</a> <a href="https://github.com/maxkratt/fish-networking-starting-connections" class="button secondary" data-icon="github">Repository</a>
{% endhint %}

[^1]: Or any other object in the scene you prefer.
