# NullReferenceException：Physics.OverlapSphereNonAlloc 返回了空的结果数组

- **ID:** `unity/physics-overlap-sphere-null`
- **领域:** unity
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

传递给 OverlapSphereNonAlloc 的结果数组在调用方法之前为 null 或未初始化。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2020.3.48f1 | active | — | — |
| 2021.3.30f1 | active | — | — |
| 2022.3.10f1 | active | — | — |

## 解决方案

1. ```
   Initialize the results array before the call: `Collider[] results = new Collider[10]; int count = Physics.OverlapSphereNonAlloc(center, radius, results);`
   ```
2. ```
   Check if results is null before passing: `if (results == null) results = new Collider[10]; int count = Physics.OverlapSphereNonAlloc(center, radius, results);`
   ```

## 无效尝试

- **** — The null check doesn't prevent the NullReferenceException from being thrown when accessing the uninitialized array in the method call itself. (95% 失败率)
- **** — The error is about the array being null, not its size; size is irrelevant if the array reference is null. (99% 失败率)
- **** — OverlapSphere allocates a new array each call, which may fix the immediate null issue but introduces GC pressure and doesn't solve the root cause of uninitialized array. (30% 失败率)
