# NullReferenceException: CanvasRaycaster: 设置为屏幕空间-叠加的 Canvas 未找到相机

- **ID:** `unity/ui-canvas-raycaster-null`
- **领域:** unity
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 94%

## 根因

屏幕空间-叠加模式的 Canvas 需要相机来渲染射线检测，但事件相机为 null 或 Canvas 未分配给任何相机。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Unity 2022.3 | active | — | — |
| Unity 2023.1 | active | — | — |
| Unity 2023.2 | active | — | — |
| Unity 6000.0 | active | — | — |

## 解决方案

1. ```
   Assign the main camera to the canvas's 'Event Camera' field in the inspector or via code: canvas.worldCamera = Camera.main;
   ```
2. ```
   Change the canvas render mode to 'Screen Space - Overlay' and ensure there is at least one camera in the scene tagged as 'MainCamera'.
   ```

## 无效尝试

- **** — Screen Space - Camera requires a camera assignment; the error persists if no camera is set. (80% 失败率)
- **** — The null reference still occurs if the canvas is enabled at any point during the frame. (70% 失败率)
