Skip to content

Panic when attempting to spawn scene from component spawned by scene #23731

@Semihazah12

Description

@Semihazah12

Bevy version and features

Bevy Main (0.19.dev, rev#83a067d4)
Features:

  • "bevy_scene"

  • "bevy_asset"

  • "debug"

  • the Rust version you're using (you can get this by running cargo --version)

    • cargo 1.96.0-nightly (a357df4c2 2026-04-03)
  • the operating system or browser used, including its version

    • CachyOS

What you did

Used a bsn scene to spawn an entity with a component with a component hook that spawn another scene;

What went wrong

Expected: Both scenes to spawn successfully.
Result: Panic with the following message:

thread 'main' (113688) panicked at /.cargo/git/checkouts/bevy-50d7e162b728c6c6/83a067d/crates/bevy_ecs/src/error/handler.rs:130:1:
Encountered an error in observer `bevy_scene::spawn::on_add_scene_patch_instance`: Parameter `ResMut<'_, QueuedScenes>` failed validation: Resource does not exist
If this is an expected state, wrap the parameter in `Option<T>` and handle `None` when it happens, or wrap the parameter in `If<T>` to skip the system when it happens.

Additional information

Minimal Reproducible Sample:

use bevy::{
    ecs::{lifecycle::HookContext, world::DeferredWorld},
    prelude::*,
};
fn main() {
    let mut app = App::new();
    app.add_plugins(DefaultPlugins);
    app.add_systems(Startup, startup);
    app.run();
}

fn startup(mut commands: Commands) {
    commands.queue_spawn_scene(test_scene_1());
}

fn test_scene_1() -> impl Scene {
    bsn!(SpawnScene2OnInsert)
}

fn test_scene_2() -> impl Scene {
    bsn!(#Name)
}

#[derive(Component, Default, Clone)]
#[component(on_insert = Self::on_insert)]
struct SpawnScene2OnInsert;

impl SpawnScene2OnInsert {
    fn on_insert(mut world: DeferredWorld, _context: HookContext) {
        world.commands().queue_spawn_scene(test_scene_2());
    }
}

Potential Cause:
queue_spawn_scene inserts a ScenePatchInstance, which then adds the scene handle to QueuedScenes via an observer. When a queued scene is spawned, the world accesses QueuedScenes via resource_scope, taking it out of the world temporarily. When SpawnScene2OnInsert is inserted, it queues a command which is executed immediately, before QueuedScenes is return into the world, causing a panic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ScenesComposing and serializing ECS objectsC-BugAn unexpected or incorrect behaviorP-CrashA sudden unexpected crashS-Needs-DesignThis issue requires design work to think about how it would best be accomplished

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions