Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


InstantiateGameObjects

Creates a specified number of instances of a GameObject identified by its instance ID and populates NativeArrays with the EntityIds of the new GameObjects and their Transform components.
Read time 2 minutesLast updated 2 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

InstantiateGameObjects(int, int, NativeArray<int>, NativeArray<int>, Scene)

Creates a specified number of instances of a GameObject identified by its instance ID and populates NativeArrays with the EntityIds of the new GameObjects and their Transform components.
Warning
Removed. Obsolete. Please use GameObject.InstantiateGameObjects(EntityId, int, NativeArray\<EntityId\>, NativeArray\<EntityId\>, Scene) instead.
public static void InstantiateGameObjects(int sourceInstanceID, int count, NativeArray<int> newInstanceIDs, NativeArray<int> newTransformInstanceIDs, Scene destinationScene = default)

Parameters

sourceInstanceID

The EntityId of the GameObject to create additional instances of.

count

The number of instances of the GameObject to create.

newInstanceIDs

Pre-allocated NativeArray to populate with the EntityIds of the new GameObjects. Must be the same size as
count
.

newTransformInstanceIDs

Pre-allocated NativeArray to populate with the EntityIds of the Transforms of the new GameObjects. Must be the same size as
count
.

destinationScene

The Scene to place the instantiated GameObjects into. If default, then the GameObjects will be added to the currently active Scene.

Remarks

Use
InstantiateGameObjects
to instantiate multiple GameObjects as a batch. An EntityId can be resolved to an object using Resources.EntityIdToObject.
using System;using Unity.Collections;using UnityEngine;public class InstantiateEntityId : MonoBehaviour{public GameObject prefab;public int count = 100; EntityId m_EntityId; NativeArray<EntityId> m_EntityIds; NativeArray<EntityId> m_TransformIds; void Start() { m_EntityId = prefab.GetEntityId(); m_EntityIds = new NativeArray<EntityId>(count, Allocator.Persistent); m_TransformIds = new NativeArray<EntityId>(count, Allocator.Persistent); GameObject.InstantiateGameObjects(m_EntityId, count, m_EntityIds,m_TransformIds ); } void OnDestroy() { m_EntityIds.Dispose(); m_TransformIds.Dispose(); }}

InstantiateGameObjects(EntityId, int, NativeArray<EntityId>, NativeArray<EntityId>, Scene)

Creates a specified number of instances of a GameObject identified by its instance ID and populates NativeArrays with the EntityIds of the new GameObjects and their Transform components.
public static void InstantiateGameObjects(EntityId sourceEntityId, int count, NativeArray<EntityId> newEntityIds, NativeArray<EntityId> newTransformEntityIds, Scene destinationScene = default)

Parameters

sourceEntityId


count

The number of instances of the GameObject to create.


newTransformEntityIds


destinationScene

The Scene to place the instantiated GameObjects into. If default, then the GameObjects will be added to the currently active Scene.

Remarks

Use
InstantiateGameObjects
to instantiate multiple GameObjects as a batch. An EntityId can be resolved to an object using Resources.EntityIdToObject.
using System;using Unity.Collections;using UnityEngine;public class InstantiateEntityId : MonoBehaviour{public GameObject prefab;public int count = 100; EntityId m_EntityId; NativeArray<EntityId> m_EntityIds; NativeArray<EntityId> m_TransformIds; void Start() { m_EntityId = prefab.GetEntityId(); m_EntityIds = new NativeArray<EntityId>(count, Allocator.Persistent); m_TransformIds = new NativeArray<EntityId>(count, Allocator.Persistent); GameObject.InstantiateGameObjects(m_EntityId, count, m_EntityIds,m_TransformIds ); } void OnDestroy() { m_EntityIds.Dispose(); m_TransformIds.Dispose(); }}