FyroxEngine/Fyrox · critical
Type mismatch!
Error message
Type mismatch!
What it means
ResourceDataRef::deref casts the internally stored resource data (as &dyn Any) down to the requested type T and panics if the cast fails. This means the concrete Resource<T> handle being dereferenced does not actually hold data of type T — a programming/type confusion bug, not a load failure.
Solutions
- Check the resource's actual type before dereferencing (e.g. untyped.procedural_data_source / instance_id type, or use UntypedResource::try_cast::<T>() and handle None).
- Ensure the Resource<T> handle came from the correctly typed load call (resource_manager.request::<Model, _>(path) etc.).
- In generic code, use downcast_ref::<T>() manually with a graceful error instead of blindly dereferencing.
Example fix
// before
let model: UntypedResource = ...;
let data = model.try_cast::<Texture>().unwrap();
let tex = data.data_ref(); // panics: actually a Model
// after
if let Some(tex_res) = model.try_cast::<Texture>() {
let tex = tex_res.data_ref();
} else {
Log::err("Resource is not a Texture");
} Defensive patterns
Strategy: type-guard
Validate before calling
if untyped.try_cast::<T>().is_none() {
// handle wrong-typed resource before calling data_ref()
} Type guard
fn is_of_type<T: ResourceData>(r: &UntypedResource) -> bool { r.try_cast::<T>().is_some() } Prevention
- Use try_cast instead of casting and dereferencing directly
- Keep resources in their concrete typed handles
When it happens
Trigger: Dereferencing a Resource<T> whose handle was produced from a differently-typed resource (e.g. via UntypedResource::try_cast or clone of a resource of another type), or after unsafely mixing typed/untyped resource APIs.
Common situations: Casting an UntypedResource to the wrong model type and calling data_ref(); passing a Texture where a Model is expected; generic code that lost the concrete type of the resource.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cast to failed!
- Cast to failed!
- Resource type mismatch. Expected
- Unable to get a resource of type
- Unable to get a resource of type
AI-assisted analysis of FyroxEngine/Fyrox@76c91aad8e (2026-09-10).
Data as JSON: /api/errors/ede2d49d21f55759.
Report an issue: GitHub.
Appendix: source
Thrown at fyrox-resource/src/lib.rs:623
panic!(
"Attempt to get reference to resource data while it is loading! Type {}",
std::any::type_name::<T>()
)
}
ResourceState::LoadError {
ref path,
ref error,
} => {
let path = if path.as_os_str().is_empty() {
"Unknown".to_string()
} else {
format!("{path:?}")
};
panic!("Attempt to get reference to resource data which failed to load! Type {}. Path: {path}. Error: {error:?}", std::any::type_name::<T>())
}
ResourceState::Ok { ref data } => (data.inner_ref() as &dyn Any)
.downcast_ref()
.expect("Type mismatch!"),
}
}
}
impl<T> DerefMut for ResourceDataRef<'_, T>
where
T: TypedResourceData,
{
fn deref_mut(&mut self) -> &mut Self::Target {
let header = &mut *self.guard;
match header.state {
ResourceState::Unloaded => {
panic!("Attempt to get reference to resource data while it is unloaded!")
}
ResourceState::Pending { .. } => {
panic!("Attempt to get reference to resource data while it is loading!")
}
ResourceState::LoadError { .. } => {View on GitHub (pinned to 76c91aad8e)