Immerse SDK
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Instantiation

To instantiate a NetworkBehaviour prefab at runtime, it must first be registered in Network Prefabs list in the project multiplayer settings.

Prefab list

Once registered, a prefab can be instantiated via the Instantiate method in the INetworkClient. This method returns an spawn ID. All instantiated NetworkBehaviours have an InstantiationId. This is a value that identifies the instantiation request and can be used later for destroying the instance.

using ImmerseSDK.Multiplayer;

// Network instantiate _prefab
var spawnId = ImmerseMultiplayer.Client.Instantiate(_prefab, position, rotation, false);


// Destroy instance
ImmerseMultiplayer.Client.Destroy(spawnId);

Extensions are included that provide overloads for the Instantiate and InstantiateAsync methods that make the position, rotation and dontDestroyOnLoad parameters optional.

All instantiated NetworkBehaviours will be owned by the user that instantiated them and will be instantiated into the current active scene, similar to Unity’s Instantiate method.

Async

Alternatively, prefabs can be instantiated asynchronously. This approach will return the newly created instance but can only be used in an async method.

using ImmerseSDK.Multiplayer;

// Async network instantiate _prefab
var instance = await ImmerseMultiplayer.Client.InstantiateAsync(_prefab, position);


// Destroy instance
ImmerseMultiplayer.Client.Destroy(instance);