# Spawn prefabs

You can spawn prefabs at runtime while the game is being played. This can be useful to for procedurally adding more complex objects to the scene. For example, if you have a house created in a prefab made up of many different meshes and even actors that reference other actors within the prefab, you can spawn all of this together.

To spawn a prefab, you first need to get the prefab asset from the AssetLoader service. With the prefab, you can call the `spawnPrefab` function with optional parameters for world position and rotation. This method is asynchronous and returns a promise as it loads any resources necessary to create the prefab and calls any actors `onInit` method. The returned promise contains a PrefabInstance which later can be used to remove the prefab, removing any objects included in the prefab from the scene and removing actors.

```typescript
import { Service, GameInstance, inject, World, AssetLoader } from '@hology/core/gameplay';
import { Vector3 } from 'three';

@Service()
class Game extends GameInstance {
  private world = inject(World)
  private assetLoader = inject(AssetLoader)

  async onStart() {
    const prefab = await this.assetLoader.getPrefabByName('test prefab')
    const instance = await this.world.spawnPrefab(prefab, new Vector3(0,0,0))

    setTimeout(() => {
      this.world.removePrefab(instance)
    }, 1000)
  }
}

export default Game

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hology.app/gameplay/world/spawn-prefabs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
