instantiatePrefab allows you to instantiate a copy of a Prefab stored in the project
into the game at runtime.
A Prefab is a JSON file located in the project's prefab list:
prefabs/NAME.json
The engine loads that JSON, reconstructs the object, normalizes all its values,
loads its script, registers it internally, and immediately executes Start().
await instantiatePrefab(prefabName, x, y);
"EnemyBasic").Each prefab is a JSON file containing all object data:
// Instantiate the "EnemyBasic" prefab at position (200, 150)
const enemy = await instantiatePrefab("EnemyBasic", 200, 150);
This creates a fully functional copy of the prefab, with its script active if it has one.
function Start(obj) {
// Create an enemy next to the player when the game starts
instantiatePrefab("ENEMY_FOLLOW", obj.x + 50, obj.y);
}
function Shoot(obj) {
const bullet = await instantiatePrefab("BulletPrefab", obj.x, obj.y);
bullet.vx = 400;
}
instantiateObject, the Prefab already includes components, animations, sounds and script.| Features | instantiatePrefab | instantiateObject |
|---|---|---|
| Uses project JSON | β Yes | β No |
| Includes components | β Yes | β Only if provided manually |
| Includes default image/animations | β Yes | β No |
| Includes associated script | β Yes | β Only if provided |
| Automatically runs Start | β Yes | β Yes (new system) |
| Ideal for⦠| enemies, projectiles, repeated objects | one-off or procedural creation |