Getting started with Unity Multiplayer Networking — Part 1

Thomas Steffen
3 min readSep 11, 2023

--

Using Unity version 2021.3.1 or higher, go to the package manager and install Netcode for GameObjects and Multiplayer Tools. If you are using an older version of Unity, it may not show up and/or it may not be compatible.

Create an empty game object, name it Network Manager, attach the Network Manager script to it found under Add Component > Netcode > Network Manager.

Within the inspector, change Network Transport. There should be a dropdown to select UnityTransport. For the purpose of this tutorial, I will be using this.

Next I created an empty object named Player. As a child object of that, I created a capsule to represent our character model. Clicking back on the parent object, Player, I am going to add the component NetworkObject.

We will also need to make the Player into a Prefab. For organization, I created a Prefabs folder and a Players subfolder, placing the prefab within.

In the Network Manager, we can now place our Player Prefab in the parameter for ‘Player Prefab’. This will spawn the same prefab for each player. You can either define the visuals and code for each player from there or choose not to use this and code your players another way. For this tutorial, I will be applying our prefab here. You should also remove the Player we created in the Scene’s hierarchy, as we will be using the prefab moving forward.

You may notice there is a section in the Network Manager called Network Prefabs. Any objects you want to be shared on the network needs to be in here. If they are not, they will not be shared on the network. For now, I will be dragging the Player prefab into here as well.

If you test run this, you will see three options further down:
- Start Host — Client and Server, for example, players connecting to a host.
- Start Server — Dedicated server
- Start Client — Client side only, no connection to a server
Depending on your needs, you will be using these for testing purposes.

For ease to test during builds, I made a UI canvas with 3 buttons, I anchored this to the top right corner of the screen. Server, Host, and Client buttons, along with setting the references to the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using Unity.Netcode;
using UnityEngine;

public class NetworkManagerUI : MonoBehaviour
{
[SerializeField] private Button Btn_Server;
[SerializeField] private Button Btn_Host;
[SerializeField] private Button Btn_Client;

private void Awake()
{
//Lamda expression / Delegate
Btn_Server.onClick.AddListener(() => {
NetworkManager.Singleton.StartServer();
});

//Lamda expression / Delegate
Btn_Host.onClick.AddListener(() => {
NetworkManager.Singleton.StartHost();
});

//Lamda expression / Delegate
Btn_Client.onClick.AddListener(() => {
NetworkManager.Singleton.StartClient();
});
}
}

Under the Network Manager you can set Log Level to Developer, this enables us to see more logs pertaining to our network. If you aren’t using a 3rd party asset to see it live, you can find your logs within your file system:

Player-related log locations

To view the Player log, open a Console Window (menu: Window > General > Console) and select Open Player Log from the Console window menu. You can also navigate to the following folder:

Operating systemPlayer log
locationAndroid
To access the Player log for an Android application, use Android logcat. For more information, see View Android logs.iOSUse the GDB console, or the Organizer Console through XCode to access iOS device logs. For more information on device logs, see Apple’s documentation.Linux~/.config/unity3d/CompanyName/ProductName/Player.logmacOS~/Library/Logs/Company Name/Product Name/Player.log

Note: You can also use the Console.app utility to find the log file.Universal Windows Platform%USERPROFILE%\AppData\Local\Packages\<productname>\TempState\UnityPlayer.logWebGL
Unity writes the log output to your browser’s JavaScript console.Windows%USERPROFILE%\AppData\LocalLow\CompanyName\ProductName\Player.log

Now you can Build your game and test things out. I ran a build and also launched in my Unity, one application used the Host, the other used the Client. I was able to join in and see two players.

In the next article, we’ll get our characters moving and synced up!

--

--

Thomas Steffen

I am Virtual Reality Developer, UI Systems, and general programmer with a passion for Unity software development.