Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/facepunch/sbox-public/llms.txt

Use this file to discover all available pages before exploring further.

Scene is the top-level container for all GameObjects in s&box. It extends GameObject, so it shares the same hierarchy and component APIs, while adding scene-level concerns: loading from disk, finding objects and components across the entire hierarchy, physics world access, and scene events. This page documents the public API you interact with at runtime.
The active scene is always accessible via Game.ActiveScene. Most of the time you get a Scene reference through Component.Scene or GameObject.Scene.

Static members

Scene.All
IEnumerable<Scene>
All active, non-editor scenes currently loaded. Useful when running multiple additive scenes simultaneously.
foreach (var scene in Scene.All)
{
    Log.Info(scene.Name);
}

Key properties

IsEditor
bool
true when this is an editor scene (used for editing in the tooling, not for gameplay). Components with ExecuteInEditor still run; all others do not.
IsValid
bool
true as long as the scene has not been destroyed. A destroyed scene has no SceneWorld.
IsLoading
bool
true while loading tasks registered by ISceneLoadingEvents.OnLoad are still pending. The loading screen remains visible while this is true.
Source
GameResource
The SceneFile resource this scene was last loaded from, or null if the scene was created in code.
WantsSystemScene
bool
When true (the default), the project’s system scene is additively loaded on top of this scene. Set to false for scenes that should not inherit system-scene content (e.g. bare menu scenes).
PhysicsWorld
PhysicsWorld
The physics simulation for this scene. Lazily created on first access. Use it to configure gravity, run trace queries, and access physics bodies.
Scene.PhysicsWorld.Gravity = Vector3.Down * 500f;
SceneWorld
SceneWorld
The render world associated with this scene. Renderable SceneObjects (models, lights, etc.) are registered here.
RenderAttributes
RenderAttributes
Global render attributes accessible by any renderable in this scene. Use to pass per-scene shader parameters.
Directory
GameObjectDirectory
Internal index of all GameObjects and components in the scene. Prefer FindAllWithTag, GetAll<T>, and Get<T> over accessing this directly.
Volumes
VolumeSystem
A fast lookup system for components that implement a volume (bounding box). Use Volumes.FindAll<T>(bounds) to query overlapping volumes efficiently.

Constructor

public Scene()
Creates a new empty scene. You rarely need to call this directly in game code — instead load a SceneFile via Game.ChangeScene() or call Load() on the active scene.

Loading a scene

Load() and LoadFromFile() load content into the current scene object. They do not replicate the change to other players in a multiplayer session. Use Game.ChangeScene() to change the scene for all connected clients.

Load(GameResource)

public virtual bool Load(GameResource resource)
Loads a SceneFile resource into this scene. Clears existing content unless the resource is an additive load.
resource
GameResource
required
A SceneFile obtained from ResourceLibrary or a [Property] reference.
[Property] public SceneFile NextScene { get; set; }

void GoToNextScene()
{
    Game.ActiveScene.Load(NextScene);
}

Load(SceneLoadOptions)

public bool Load(SceneLoadOptions options)
Full-control overload. SceneLoadOptions lets you configure additive loading, system scene suppression, and the loading screen.
var opts = new SceneLoadOptions();
opts.IsAdditive = true;
opts.SetScene("scenes/village.scene");
Game.ActiveScene.Load(opts);

LoadFromFile

public bool LoadFromFile(string filename)
Loads a scene from a resource path string.
filename
string
required
Resource path, e.g. "scenes/main.scene".

Creating GameObjects

CreateObject

public GameObject CreateObject(bool enabled = true)
Creates a new GameObject parented to this scene. This method pushes the scene as the active scene for the duration of construction, so components created inside the constructor target the correct scene even when called from an inactive scene context.
enabled
bool
default:"true"
Whether the new object starts enabled.
var go = Game.ActiveScene.CreateObject();
go.Name = "Spawned Object";
go.AddComponent<MyBehaviour>();

Finding GameObjects and components

GetAll<T>

public IEnumerable<T> GetAll<T>()
public void GetAll<T>(List<T> target)
Returns all active objects in the scene that implement or extend T. Works for components, interfaces, and GameObjectSystem subtypes. The results come from a fast type-indexed cache — this is the preferred way to iterate over all components of a given type.
// Find all active Rigidbody components in the scene
foreach (var rb in Scene.GetAll<Rigidbody>())
{
    rb.ApplyForce(Vector3.Up * 100f);
}
The list overload avoids allocation when called frequently:
var lights = new List<PointLight>();
Scene.GetAll(lights);

Get<T>

public T Get<T>()
Returns the first active object of type T in the scene, or default when none is found. Useful for singletons and global systems.
var gameManager = Scene.Get<GameManager>();

FindAllWithTag

public IEnumerable<GameObject> FindAllWithTag(string tag)
Returns all GameObjects in the scene that have the given tag (including inherited tags from ancestors).
foreach (var enemy in Scene.FindAllWithTag("enemy"))
{
    enemy.Destroy();
}

FindAllWithTags

public IEnumerable<GameObject> FindAllWithTags(IEnumerable<string> tags)
Returns all GameObjects that have all of the specified tags.
var heavyEnemies = Scene.FindAllWithTags(["enemy", "heavy"]);

Scene events

RunEvent<T>

public override void RunEvent<T>(Action<T> action, FindMode find = FindMode.EnabledInSelfAndDescendants)
Calls action on every enabled object in the scene that implements interface T. Uses the fast type index — preferred over iterating GetAll<T>() manually.
Scene.RunEvent<IPlayerEvents>(x => x.OnRoundStart());

ISceneEvent<T>

ISceneEvent<T> provides static helper methods so your event interface can post to the entire scene without needing a direct Scene reference.
// Declare your event interface
public interface IDamageEvents : ISceneEvent<IDamageEvents>
{
    void OnDamageTaken(GameObject victim, float amount);
}

// Post from anywhere
IDamageEvents.Post(x => x.OnDamageTaken(this.GameObject, 50f));

// Post to a specific GameObject and its descendants only
IDamageEvents.PostToGameObject(targetGo, x => x.OnDamageTaken(targetGo, 50f));
ISceneEvent.Post(Action<T> action)
static void
Broadcasts the event to the entire active scene.
ISceneEvent.PostToGameObject(GameObject go, Action<T> action, FindMode find)
static void
Broadcasts the event to a specific GameObject and optionally its descendants.

Batching

BatchGroup

public IDisposable BatchGroup()
Collects OnEnabled/OnDisabled callbacks and network spawn messages created inside the scope and dispatches them in a deterministic order when the scope is disposed. Use when spawning multiple objects at once to ensure they can find each other during OnAwake/OnEnabled.
using (Scene.BatchGroup())
{
    var a = new GameObject("A");
    a.AddComponent<ComponentA>();

    var b = new GameObject("B");
    b.AddComponent<ComponentB>();
    // ComponentA and ComponentB can reference each other in OnEnabled
}

Push

public IDisposable Push()
Sets this scene as Game.ActiveScene for the duration of the scope. Automatically restored when the scope is disposed. Used internally by many engine methods.
using (myScene.Push())
{
    // Game.ActiveScene == myScene here
    var go = new GameObject("Temp");
}

Lifecycle

Destroy

public override void Destroy()
Tears down the scene: clears all GameObjects and components, deletes the PhysicsWorld and SceneWorld, and removes the scene from Scene.All. After this call, IsValid is false. Do not use the scene reference after calling Destroy().

Loading screen and tasks

When a scene loads, registered ISceneLoadingEvents.OnLoad handlers can return a Task to keep the loading screen visible until async work completes (e.g. generating a NavMesh, downloading content).
IsLoading
bool
true while loading tasks are still pending.
StartLoading()
void
Begins the loading task tracking loop. Called automatically when Load() is invoked outside of the editor.

See also

GameObject

The fundamental scene object that components attach to.

Component

The base class for all game scripts.

Physics

Guide to using PhysicsWorld, traces, and rigidbodies.

Build docs developers (and LLMs) love