InstantiateOptions

Namespace:

namespace Lenga\Engine\Core;

class InstantiateOptions

InstantiateOptions describes how a prefab, GameObject, or component source should be instantiated.

Properties

name

public ?string $name

Optional display name for the created instance.

position

public ?Vector3 $position

Optional world-space position for the created instance.

rotation

public ?Quaternion $rotation

Optional world-space rotation for the created instance.

parent

public GameObject|Transform|null $parent

Optional parent for the created instance.

worldPositionStays

public bool $worldPositionStays

Controls how a parent-only instantiate keeps transform values. Use false to keep the source local transform under the new parent. Use true to preserve the source world transform.

Methods

named

public static function named(string $name): self

Creates options that only rename the created instance.

at

public static function at(
    Vector3 $position,
    Vector3|Quaternion|null $rotation = null,
    GameObject|Transform|null $parent = null,
    ?string $name = null,
): self

Creates options for spawning at a world position, with optional world rotation, parent, and name. Passing a Vector3 as the rotation treats it as Euler angles in degrees.

under

public static function under(
    GameObject|Transform|null $parent,
    bool $worldPositionStays = false,
    ?string $name = null,
): self

Creates options for instantiating under a parent.

Example

use Lenga\Engine\Core\GameObject;
use Lenga\Engine\Core\InstantiateOptions;
use Lenga\Engine\Core\Vector3;

$spark = GameObject::instantiate(
    $this->hitSparkPrefab,
    InstantiateOptions::at(
        position: $hitPoint,
        rotation: new Vector3(0, 45, 0),
        name: 'Hit Spark',
    ),
);