Network Identity
The NetworkIdentity component enables you to spawn GameObjects
on the server and have them automatically appear on each client.
Overview
export default class CubeManager extends AirshipSingleton {
// This is a reference to a prefab with a NetworkIdentity component
public Cube: GameObject;
public override Start(): void {
if (Game.IsServer()) {
// Spawn a cube on the server and replicate it to each player
const cube = Object.Instantiate(
this.Cube,
new Vector3(0, 1, 0),
Quaternion.identity
);
NetworkServer.Spawn(cube);
// Despawn cube after 4 seconds
task.delay(4, () => NetworkServer.Destroy(cube));
}
}
}
NetworkIdentity
Attaching a NetworkIdentity Component
The NetworkIdentity
component can be added to any GameObject
through the "Add Component" menu.

Adding a NetworkIdentity to a NetworkPrefabCollection
In order to be able to spawn and despawn a NetworkIdentity
through code, it must be a part of a game's NetworkPrefabCollection. The template project includes a collection in the Assets/Resources
folder.
If your project does not already contain a collection, you can automatically generate one through the "Generate Network Prefab Collections" menu item.

To add a NetworkIdentity
to your game's NetworkPrefabCollection, drag a prefab from your Hierarchy or Project tab onto the NetworkPrefabCollection "Network Prefabs" list, or select the prefab through the search menu.

Spawning a NetworkIdentity
const cube = Object.Instantiate(cubePrefab);
NetworkServer.Spawn(cube);
When a player joins a game all networked GameObjects
in the server scene are automatically replicated to the player.
Despawning a NetworkIdentity
NetworkServer.Destroy(cube);
Sending a NetworkIdentity over network
You can use networkIdentity.netId
to send a reference to a NetworkIdentity via NetworkSignals and NetworkFunctions.
Use NetworkUtil.GetNetworkIdentity(netId)
to retrieve a NetworkIdentity from the netId.
Last updated