✨ instantiateObject

What is it?

The instantiateObject function allows you to create a completely new object during gameplay. This method is similar to instantiating a prefab in Unity, but generating the object from a configuration object instead of a prefab file.

Unlike previous versions of the engine, instantiateObject now includes:

This ensures that the instantiated object behaves exactly like any object loaded from the scene or from a prefab.

📖 Syntax

await instantiateObject(config, optionalScript);

📦 Allowed properties in config

🚀 What happens automatically?

When calling instantiateObject, the engine performs:

📘 Simple example


// Create a red enemy at position (300, 200)
const enemy = await instantiateObject({
  type: "enemy",
  name: "Enemy1",
  x: 300,
  y: 200,
  color: "#f00"
}, "EnemyAI.js");

📘 Example with image and components


const bullet = await instantiateObject({
  type: "bullet",
  x: player.x,
  y: player.y,
  image: "bullet.png",
  scaleX: 1,
  scaleY: 1,
  components: [
    { type: "AudioSource", clip: "shoot.wav", playOnStart: true }
  ]
}, "BulletController.js");