Skip to main content

GameObject

GameObject provides scene lookup, active-state control, tags, layers, and bounded calls between Lua Behaviours. IDs are integers owned by the scene; cache resolved IDs and validate them with GameObject.Exists when lifetime may have changed.

Lookup and State

MethodReturnsDescription
GameObject.Find(name)integerFinds a GameObject by exact name, including inactive objects, or -1.
GameObject.Exists(gameObjectId)booleanChecks whether an ID is valid.
GameObject.GetName(gameObjectId)string or nilReads the display name.
GameObject.IsActive(gameObjectId)boolean or nilReads the active state.
GameObject.SetActive(gameObjectId, active)booleanChanges the active state.

Changing active state dispatches OnEnable or OnDisable only when the state actually changes.

local gateGameObjectId = -1

function Start(gameObjectId)
gateGameObjectId = GameObject.Find("Security Gate")
if not GameObject.Exists(gateGameObjectId) then
Log.Print("Security Gate was not found")
end
end

function OpenGate()
if GameObject.Exists(gateGameObjectId) then
GameObject.SetActive(gateGameObjectId, false)
end
end

Tags and Layers

MethodReturnsDescription
GameObject.CompareTag(gameObjectId, tag)booleanCompares the baked tag without allocating.
GameObject.GetLayer(gameObjectId)integerReturns the physics/render layer.
GameObject.SetLayer(gameObjectId, layer)booleanSets a layer inside the project's baked range.

Use CompareTag in collision callbacks instead of reading and comparing names. Layers are also accepted by Physics.Raycast through its optional layer mask.

Calling Another Lua Behaviour

GameObject.Invoke(
targetGameObjectId,
"Open",
sourceGameObjectId,
0,
"keycard",
"accepted"
)

GameObject.Invoke returns false when the target, Lua Behaviour, or function does not exist, or the call cannot run within the scripting budget. It supports up to two integer and two string arguments after the function name. The target function receives its own GameObject ID first:

function Open(gameObjectId, sourceGameObjectId, unusedValue, itemName, result)
Log.Print(itemName .. " was " .. result)
end

UI Buttons, Interactables, and Timeline script events use the same exact-name callback model. Custom callback names are project-defined; engine lifecycle and API names remain PascalCase.

GameObject.Find can find inactive objects. Use GameObject.IsActive when you need to check their state. Keep names unique because lookup returns the first match, and find objects again after changing scenes.