unity runtime_error ai_generated true

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

NullReferenceException: CanvasRaycaster: No camera found for canvas that is set to Screen Space - Overlay

ID: unity/ui-canvas-raycaster-null

其他格式: JSON · Markdown 中文 · English
94%修复率
86%置信度
1证据数
2024-05-14首次发现

版本兼容性

版本状态引入弃用备注
Unity 2022.3 active
Unity 2023.1 active
Unity 2023.2 active
Unity 6000.0 active

根因分析

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

English

A Canvas in Screen Space - Overlay mode requires a camera to render raycasts, but the event camera is null or the canvas is not assigned to any camera.

generic

官方文档

https://docs.unity3d.com/ScriptReference/Canvas-worldCamera.html

解决方案

  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'.

无效尝试

常见但无效的做法:

  1. 80% 失败

    Screen Space - Camera requires a camera assignment; the error persists if no camera is set.

  2. 70% 失败

    The null reference still occurs if the canvas is enabled at any point during the frame.