Your First Networked Game "Hello World"
A "Hello World" program is a computer program that outputs or displays the message "Hello, World!". Normally it is the first program written by people learning to code. It is also used as a sanity test to make sure that a computer language is correctly installed, and that the operator understands how to use it.
This "Hello World" tutorial walks you through creating a project, installing the Netcode for GameObjects (Netcode) package, and creating the basic components for your first networked game.
#
RequirementsThis tutorial requires an Netcode-supported version of Unity (2019.4+).
#
Create a new project in Unity- Open the Unity Hub.
- Click New.
- Select type ‘3D’
- Rename the project "Hello World".
- Select the location to save the project.
#
Import Netcode Package via URLSee the Install Netcode guide to install the Netcode package.
#
Create the Basic ComponentsIn this section we will create the basic building blocks of a multiplayer game.
#
Creating Network Manager and selecting the TransportIn this section we will add a Network Manager and add a Transport to our project.
Right click in the Hierarchy tab of the Main Unity Window.
Select Create Empty.
Rename the
GameObject
NetworkManager.tip
We renamed the
GameObject
because:- It makes it easier to refer to later.
- There should only be one NetworkManager, this is the object that contains the
NetworkManager
component. You may get unexpected results if you create more than one NetworkManager.
You have now created a new
GameObject
called NetworkManager.Select NetworkManager.
Click Add Component in the Inspector Tab.
Select Netcode from the list shown.
Select
NetworkManager
Component from the list displayed.Inside the
NetworkManager
component tab, locate theNetworkTransport
field.Click "Select Transport".
Select
UnetTransport
.Save your scene.
#
Creating an object to spawn for each connected playerThis section adds in a player object and spawns it for each connected player.
Create a 3D Object->Capsule
Rename it Player.
Add a
NetworkObject
component.Click the Assets folder.
Create a new Folder and call it Prefabs.
Make Player a prefab by dragging it to Prefabs folder you just created.
Delete Player from scene.
tip
We remove Player, because we will be using the network library to spawn the player. The library cannot track objects that start in the scene.
Select
NetworkManager
.Inside the
NetworkManager
component tab, locate theNetworkPrefabs
field.Click
+
to create a slot.Drag this player prefab from above into the new empty slot
Drag the prefab also into the
Player Prefab
slot.important
When you drop the prefab into the
Player Prefab
slot, you are telling the library that when a client connects to the game, automatically spawn this prefab as the character for the connecting client. If you do not have any prefab set as thePlayer Prefab
no player object will be spawned.Create a 3D Object->Plane, centered at (0,0,0).
Save your scene
#
Testing Hello WorldNow we will test to see if everything works as expected.
- Click Play.
- Click Start Host under NetworkManager.
Congrats!
Congratulations you have created a networked game. It is not a very flashy game but it is a networked game nonetheless. For the next steps in your journey see Building on "Hello World"
Special Thanks
This guide would not have been possible without the hard work and support of Fernando Cortez, Unity.