Using Ownership to Read Values

Learn how to store values for clients and read them on unlinked objects by reading owner information.

This guide requires Share Ids to be enabled on the ServerManager for clients to read values. Share Ids is enabled by default, and does not reveal any sensitive information to clients.

We are going to demonstrate how to assign a display name to each client, and display that name on player objects.

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.

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.

Last updated