Hello,
Unity crash using OverlapSphereCommand following the documentation and adding/removing gameobjects with collider Unity crash.
How to reproduce:
- Create a new project with Unity 2022.2.13
- Add a gameobject with the following script
- Wait for crash (less than 5 seconds)
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
public class OverlapSphereCommand_Crash : MonoBehaviour {
[Min(1)]
public int instanceCount = 50;
private List<GameObject> instances;
public void Start() {
instances = new List<GameObject>();
}
// Update is called once per frame
void Update() {
BatchOverlapSphere();
if (instances.Count > instanceCount) {
Destroy(instances[0]);
} else {
instances.Add(CreateCubeCollider());
}
Debug.Log($"Cubes count: {instances.Count }");
}
private GameObject CreateCubeCollider() {
GameObject cubeGO = new GameObject();
cubeGO.transform.position = Vector3.zero;
cubeGO.AddComponent<BoxCollider>();
return cubeGO;
}
void BatchOverlapSphere() {
var commands = new NativeArray<OverlapSphereCommand>(1, Allocator.TempJob);
var results = new NativeArray<ColliderHit>(3, Allocator.TempJob);
commands[0] = new OverlapSphereCommand(Vector3.zero, 10f, QueryParameters.Default);
OverlapSphereCommand.ScheduleBatch(commands, results, 1, 3).Complete();
int hitCount = 0;
foreach (var hit in results) {
if (hit.collider == null) {
break;
}
hitCount++;
//Debug.Log(hit.collider.name);
}
Debug.Log($"HitCount: {hitCount}");
commands.Dispose();
results.Dispose();
}
}
I have sent a bug report: IN-37645 - Crash using OverlapSphereCommand and add/remove gameobjects