Using Ownership to Read Values
Learn how to store values for clients and read them on unlinked objects by reading owner information.
We are going to demonstrate how to assign a display name to each client, and display that name on player objects.
This guide assumes you already have a NetworkManager object in your scene.
PlayerNames
First make a new script on an empty scene object and name it PlayerNames; this script needs to inherit NetworkBehaviour.
After adding the script to your scene object a NetworkObject component will automatically be added to the same object. On the NetworkObject enable 'Is Global', make this object a prefab, then delete it from your scene.

Notice that on the NetworkObject we also set the Initialize Order to -128. Doing so ensures that this NetworkObject will initialize before any other object does, which promises the OnStart callbacks will execute before other scripts. This step is most likely not needed, but given this is more-or-less a managing script for player names giving it execution priority is good practice.
We are now going to populate the PlayerNames script with the following code.
The above code snippet will give players a random name when they connect, and allow clients to change their name by calling the SetName RPC.
Automatically spawning PlayerNames
After you have made the prefab select your scene NetworkManager, make a child object named ServerSpawner, and add the script ServerSpawner. You may place this script anywhere in your scene or game, but for simplicity sake we're going to nest it beneath the NetworkManager. After you add the script, insert your newly created PlayerNames prefab into the 'Network Objects' field and ensure Automatically Spawn is enabled.

Displaying player names
Next we are going to make a very simple script which changes the text value on a TextMeshPro component. This is a very simple example which might go on the players character. You could use similar scripts for chat names, and more.
Make a new script named CharacterName. Add the following:
Now when an object spawns containing the script above, the _text field will be updated to the owners player name. In addition, if the owner changes their name at any time, the text will be updated again.
Notice how we make use of NetworkManager.Register, Unregister, and GetInstance in this guide. This is a very useful feature for accessing global networked scripts.
Last updated