GameObject

Namespace:

namespace Lenga\Engine\Core;

class GameObject

GameObject represents an object in a scene hierarchy. Every GameObject has a Transform, can contain components, and can have child GameObjects.

Parenting affects transforms in the usual hierarchy-friendly way: moving a parent moves its children in world space, but moving a child does not move the parent. Child objects keep their own local position, rotation, and scale relative to the parent.

Properties

prefabAssetPath

public string $prefabAssetPath

Contains the project-relative prefab asset path when this GameObject is an inspector-assigned prefab reference rather than a live scene object. Scene objects return an empty string.

Methods

setActive

public function setActive(bool $value)

isActiveSelf

public function isActiveSelf()

isActiveInHierarchy

public function isActiveInHierarchy()

compareTag

public function compareTag(string $tag)

getScene

public function getScene()

getChildren

public function getChildren()

setParent

public function setParent(?GameObject $parent, bool $worldPositionStays = true): bool

Moves this GameObject under another GameObject, or back to the scene root when $parent is null. When $worldPositionStays is true, Lenga preserves the object's world-space placement and recalculates its local transform. When it is false, Lenga keeps the current local transform relative to the new parent.

getComponents

public function getComponents(?string $type = null)

Pass a concrete component class such as Rigidbody2D::class when you want clear return types and better editor support. String type names remain available for advanced dynamic lookups.

getComponentsInChildren

public function getComponentsInChildren(?string $type = null, bool $includeInactive = false)

getComponentInChildren

public function getComponentInChildren(string $type, bool $includeInactive = false)

getComponentsInParent

public function getComponentsInParent(?string $type = null, bool $includeInactive = false)

getComponentInParent

public function getComponentInParent(string $type, bool $includeInactive = false)

addComponent

public function addComponent(string $type)

Accepts concrete component class names and dynamic type strings. Behaviour components must be added by passing the concrete script class.

registerComponentWrapper

public static function registerComponentWrapper(string $nativeType, string $componentClass): void

Registers a component wrapper class when an advanced package does not follow Lenga's default namespace conventions.

clone

public function clone(?string $name = null)

instantiate

public static function instantiate(
    GameObject|Transform|Component|Behaviour $original,
    string|Vector3|Transform|GameObject|InstantiateOptions|array|null $optionsOrPositionOrParentOrName = null,
    Vector3|Quaternion|bool|string|null $rotationOrWorldPositionStaysOrName = null,
    Transform|GameObject|string|null $parentOrName = null,
    ?string $name = null,
): object

Creates a new scene instance from an existing scene object, a prefab asset reference, or a component reference. When the source is a component, Lenga clones the owning GameObject and returns the matching component on the clone.

Common forms:

$copy = GameObject::instantiate($source);
$copy = GameObject::instantiate($source, 'Display Name');
$copy = GameObject::instantiate($source, $position, $rotation);
$copy = GameObject::instantiate($source, $position, $rotation, $parent);
$copy = GameObject::instantiate($source, InstantiateOptions::under($parent, worldPositionStays: true));
$copy = GameObject::instantiate($source, InstantiateOptions::at($position, name: 'Hit Spark'));

find

public static function find(string $name)

findGameObjectsWithTag

public static function findGameObjectsWithTag(string $tag)

fromPrefabAssetPath

public static function fromPrefabAssetPath(string $assetPath, ?string $name = null)

Creates a prefab asset reference from a project-relative asset path. Most gameplay scripts receive these references through exposed GameObject fields in the inspector.

Example

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

class GameObjectInteraction extends Behaviour
{
    public function start(): void
    {
        // Access the GameObject this Behaviour is attached to
        $gameObject = $this->gameObject;

        // Get information about this GameObject
        Debug::log('Name: ' . $gameObject->name);
        Debug::log('Active: ' . ($gameObject->isActiveInHierarchy() ? 'yes' : 'no'));

        // Find other game objects by tag
        $enemies = GameObject::findGameObjectsWithTag('Enemy');
        foreach ($enemies as $enemy) {
            Debug::log('Found enemy: ' . $enemy->name);
        }
    }
}

Prefab Field Example

use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\GameObject;

final class ProjectileLauncher extends Behaviour
{
    public ?GameObject $projectilePrefab = null;

    public function fire(): void
    {
        if ($this->projectilePrefab === null) {
            return;
        }

        $projectile = GameObject::instantiate(
            $this->projectilePrefab,
            InstantiateOptions::at($this->transform->position),
        );
    }
}