Spawning and Despawning

Network Objects can be spawned by embedding them in scenes or dynamically at runtime using ISpaceContentService.SpawnNetworkObject().

Note

Do not use GameObject.Instantiate() to spawn network objects. This will only create the object on the local machine and the object will not initialize as expected.

Network Prefabs

A network prefab is any Unity Prefab that has a SpatialNetworkObject component on the root GameObject. They can contain multiple behaviours and even multiple nested Network Objects.

Network prefabs can also be embedded in a scene, however their ownership and lifecycle settings might be different from dynamically spawned objects since these scene objects should never be destroyed.

Registering a Network Prefab

Network prefabs are automatically assigned a unique Guid when they are created. The guid is assigned to the root GameObject's SpatialNetworkObject.networkPrefabGuid property. This guid is used to identify which prefab should be instantiated on other clients when a network object is spawned.

Before a prefab can be spawned dynamically, it needs to be registered it with the Space Package Config. This lets Spatial know which of the prefabs in the project should be included in the space. To do this, add a new element to the Network Prefabs list in the Space Package Config and drag the prefab into the slot.

Space Config Network Prefabs

Spawning a Network Prefab

Use ISpaceContentService.SpawnNetworkObject(networkPrefab, position, rotation) to spawn the prefab at runtime.

public class ColoredCubeSpawner : MonoBehaviour
{
public SpatialNetworkObject coloredCubePrefab;

private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Vector3 pos = transform.position + new Vector3(Random.Range(-5, 5), Random.Range(-5, 5), Random.Range(-5, 5));
Quaternion rot = Random.rotation;
SpatialBridge.spaceContentService.SpawnNetworkObject(coloredCubePrefab, pos, rot);
}
}
}
public class ColoredCubeSpawner : MonoBehaviour
{
public SpatialNetworkObject coloredCubePrefab;

private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Vector3 pos = transform.position + new Vector3(Random.Range(-5, 5), Random.Range(-5, 5), Random.Range(-5, 5));
Quaternion rot = Random.rotation;
SpatialBridge.spaceContentService.SpawnNetworkObject(coloredCubePrefab, pos, rot);
}
}
}

Despawning / Destroying

Dynamically spawned objects can be destroyed with GameObject.Destroy() or ISpaceContentService.DestroySpaceObject(objectID).

Scene-embedded objects should never be destroyed. Destroying them will result in errors and unexpected behavior.

Nested objects under Network Prefabs should also never be destroyed. Only the root network object can be destroyed.

Scene-Embedded vs Dynamically Spawned Objects

Network Objects can either be embedded in a scene by placing them in the scene hierarchy or spawned dynamically at runtime using ISpaceContentService.SpawnNetworkObject().

Scene-Embedded Objects

Network objects that are inside scenes are spawned by the first actor in a server instance. The actor finds all the network objects in the scene and spawns them in the network context.

The ownership of scene-embedded objects is assigned to the actor that initially spawns them.

Scene-embedded objects are considered "static objects" and should never be destroyed. Destroying them will result in errors and unexpected behavior.

When to use scene-embedded objects:

  • Objects that should always exist
  • Interactive level elements such as doors, buttons, etc that always exist
  • Game Manager network objects with logic and state for controlling game state

Dynamically Spawned Objects

Dynamically spawning objects are any network objects that are spawned at runtime using ISpaceContentService.SpawnNetworkObject().

By default, the spawning actor is the owner of the spawned object. Ownership can be transferred to other actors if needed.

In contrast to scene-embedded objects, dynamically spawned objects can be destroyed, however only root network objects can be destroyed. Destroying a nested network object will result in errors and unexpected behavior.

When to use dynamically spawned objects:

  • Enemies that can die
  • Bullets, projectiles, etc
  • Player avatars

Initialization Order

The initialization order is slightly different between scene-embedded objects and dynamically spawned objects. This is because the scene may be loaded before the network connection is established, so Spawned() calls are delayed until the network connection is established.

Scene-Embedded ObjectsDynamically Spawned Objects
Awake()Awake()
Start()Spawned()
Spawned()Start()