Skip to main content
A GameObject represents an object in the scene world. On its own, a GameObject does nothing — you add Components to it to give it behaviour, appearance, and physics.

Transform

Every GameObject has a Transform that describes where it lives in the world: its position, rotation, and scale. When a GameObject has a parent, its transform is stored relative to that parent, so moving the parent moves all children automatically.
// Set the world-space position
GameObject.WorldPosition = new Vector3( 100, 100, 100 );

// Set the position relative to the parent
GameObject.LocalPosition = new Vector3( 100, 100, 100 );

// Set the full world-space transform (position, angles, scale)
GameObject.WorldTransform = new Transform( Vector3.Zero, new Angles( 90, 90, 180 ), 2.0f );

Tags

Tags let you group and filter GameObjects. Physics uses them to decide which objects collide with each other. Cameras use them to include or exclude objects from rendering. You can also read them yourself in code:
if ( GameObject.Tags.Has( "enemy" ) )
{
    GameObject.Destroy();
}

GameObject.Tags.Add( "enemy" );
GameObject.Tags.Set( "enemy", isEnemy );
GameObject.Tags.Remove( "enemy" );
Tags are inherited. If a parent has a tag, all of its children have it too. To remove a tag from a child, you must remove it from the parent.

Hierarchy

GameObjects can have children, forming a hierarchy. Access a GameObject’s children through GameObject.Children:
foreach ( var child in GameObject.Children )
{
    Log.Info( child.Name );
}
Moving or rotating a parent automatically moves and rotates all of its children.

Creating GameObjects in the editor

In the s&box editor, right-click inside the Scene panel and choose Create GameObject to add a new empty object. You can then rename it and add components from the Inspector.

Creating GameObjects in code

Create a new empty GameObject and parent it to the scene:
var go = new GameObject();
go.Name = "My Object";
To place it in the world immediately, set its position and attach it to the active scene:
var go = new GameObject();
go.WorldPosition = new Vector3( 0, 0, 100 );

Finding GameObjects

Use Scene.Directory to look up a specific object by its GUID:
var obj = Scene.Directory.FindByGuid( guid );
Use Scene.GetAll<T>() to iterate over all components of a type — this is the most efficient way to find specific objects by their components:
foreach ( var enemy in Scene.GetAll<EnemyComponent>() )
{
    enemy.Alert();
}
You can also query the component hierarchy starting from a known GameObject — see Components for the full querying API.

Destroying a GameObject

Call Destroy() on the GameObject itself, or use DestroyGameObject() from within a component:
// From anywhere with a reference to the object
gameObject.Destroy();

// From inside a component on that object
DestroyGameObject();