{"record":{"id":"8d9a61ec6d089675","repo":"bevyengine/bevy","slug":"resource-does-not-exist","errorCode":null,"errorMessage":"resource does not exist: {}","messagePattern":"resource does not exist: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_ecs/src/world/mod.rs","lineNumber":2795,"sourceCode":"    /// world.insert_resource(A(1));\n    /// let entity = world.spawn(B(1)).id();\n    ///\n    /// world.resource_scope(|world, mut a: Mut<A>| {\n    ///     let b = world.get_mut::<B>(entity).unwrap();\n    ///     a.0 += b.0;\n    /// });\n    /// assert_eq!(world.get_resource::<A>().unwrap().0, 2);\n    /// ```\n    ///\n    /// # Note\n    ///\n    /// If the world's resource metadata is cleared within the scope, such as by calling\n    /// [`World::clear_resources`] or [`World::clear_all`], the resource will *not* be re-inserted\n    /// at the end of the scope.\n    #[track_caller]\n    pub fn resource_scope<R: Resource, U>(&mut self, f: impl FnOnce(&mut World, Mut<R>) -> U) -> U {\n        self.try_resource_scope(f)\n            .unwrap_or_else(|| panic!(\"resource does not exist: {}\", DebugName::type_name::<R>()))\n    }\n\n    /// Temporarily removes the requested resource from this [`World`] if it exists, runs custom user code,\n    /// then re-adds the resource before returning. Returns `None` if the resource does not exist in this [`World`].\n    ///\n    /// This enables safe simultaneous mutable access to both a resource and the rest of the [`World`].\n    /// For more complex access patterns, consider using [`SystemState`](crate::system::SystemState).\n    ///\n    /// See also [`resource_scope`](Self::resource_scope).\n    ///\n    /// # Note\n    ///\n    /// If the world's resource metadata is cleared within the scope, such as by calling\n    /// [`World::clear_resources`] or [`World::clear_all`], the resource will *not* be re-inserted\n    /// at the end of the scope.\n    pub fn try_resource_scope<R: Resource, U>(\n        &mut self,\n        f: impl FnOnce(&mut World, Mut<R>) -> U,","sourceCodeStart":2777,"sourceCodeEnd":2813,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/world/mod.rs#L2777-L2813","documentation":"World::resource_scope::<R, _> temporarily removes the resource R, gives your closure exclusive (&mut World, Mut<R>) access, and re-inserts R afterwards. The wrapper panics via try_resource_scope(...).unwrap_or_else when R does not exist in the World at call time, printing the type name. It is the standard way to mutate a resource and the world simultaneously, so it fails loudly when the resource was never initialized.","triggerScenarios":"Calling world.resource_scope(|w, mut r: Mut<R>| ...) before app.init_resource::<R>() / insert_resource(...) ran; calling it inside a schedule that executes before the resource's plugin (e.g. in First while the plugin inserts in PreStartup); calling it on a World where the resource was cleared with clear_resources()/clear_all(); using it as a system via IntoSystem with ResMut after the resource was removed.","commonSituations":"Forgetting to init a custom State/NextState-like resource; running tests with MinimalPlugins that omit the plugin owning the resource; ordering mistakes where a Startup system runs after a schedule that already used resource_scope; plugin refactor renaming the resource type so a different (uninitialized) type is requested.","solutions":["Initialize the resource first: app.init_resource::<R>() (needs Default) or app.insert_resource(R { ... }).","Add the plugin that provides R, or fix plugin ordering so it builds before your system runs.","For optional resources use world.try_resource_scope(...) which returns Option<U> instead of panicking.","If you cleared resources intentionally (clear_resources/clear_all inside a scope), remember they are NOT re-inserted and re-init afterwards."],"exampleFix":"// before\nworld.resource_scope(|world, mut assets: Mut<MyAssets>| { /* ... */ }); // panics\n\n// after\nworld.init_resource::<MyAssets>();\nworld.resource_scope(|world, mut assets: Mut<MyAssets>| { /* ... */ });\n\n// or, optional style:\nlet result = world.try_resource_scope(|world, mut assets: Mut<MyAssets>| assets.count);","handlingStrategy":"validation","validationCode":"if world.get_resource::<R>().is_some() {\n    world.resource_scope(|w, mut r: Mut<R>| { /* ... */ });\n} else {\n    world.init_resource::<R>();\n}","typeGuard":null,"tryCatchPattern":"let out = world.try_resource_scope(|w, mut r: Mut<R>| {\n    // ...\n}); // Option<U>: None means resource absent","preventionTips":["init_resource every resource you will scope, in plugin build().","Remember clear_resources/clear_all inside a scope will not re-insert the resource afterwards.","Use try_resource_scope when a resource is legitimately optional."],"tags":["bevy","ecs","resource","resource-scope","panic","world"],"backgroundTag":"resource-not-found","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}