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:
scriptBehaviours).This ensures that the instantiated object behaves exactly like any object loaded from the scene or from a prefab.
await instantiateObject(config, optionalScript);
configtype: type/nature of the object.name: visible name in debug (auto-generated if not provided).x, y: initial position (auto-normalized).scaleX, scaleY: scale.rotation: rotation in degrees.w, h: dimensions; if omitted, they are calculated based on scale.color: object color (if no image is used).image: image file name (path applied automatically).opacity: opacity (0–1).pixelPerfect: smoothing on/off.collision: enable collision.trigger: if acts as a trigger.collisionType: "square" or "circle".gravity: local gravity for the object.components: array of components (AudioSource, Animator, etc.).script: associated script (if not passed as second parameter).When calling instantiateObject, the engine performs:
objects array.Start, Update, etc.) in scriptBehaviours.Start(obj) immediately.
// Create a red enemy at position (300, 200)
const enemy = await instantiateObject({
type: "enemy",
name: "Enemy1",
x: 300,
y: 200,
color: "#f00"
}, "EnemyAI.js");
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");