Search Unity

Bug Crash using OverlapSphereCommand in 2022.2.13

Discussion in 'C# Job System' started by tatien, Apr 6, 2023.

  1. tatien

    tatien

    Joined:
    Jun 24, 2014
    Posts:
    5
    Hello,

    Unity crash using OverlapSphereCommand following the documentation and adding/removing gameobjects with collider Unity crash.

    How to reproduce:
    1. Create a new project with Unity 2022.2.13
    2. Add a gameobject with the following script
    3. Wait for crash (less than 5 seconds)

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using Unity.Collections;
    3. using UnityEngine;
    4.  
    5. public class OverlapSphereCommand_Crash : MonoBehaviour {
    6.     [Min(1)]
    7.     public int instanceCount = 50;
    8.  
    9.     private List<GameObject> instances;
    10.  
    11.     public void Start() {
    12.         instances = new List<GameObject>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update() {
    17.         BatchOverlapSphere();
    18.  
    19.         if (instances.Count > instanceCount) {
    20.             Destroy(instances[0]);
    21.         } else {
    22.             instances.Add(CreateCubeCollider());
    23.         }
    24.         Debug.Log($"Cubes count: {instances.Count }");
    25.  
    26.     }
    27.  
    28.     private GameObject CreateCubeCollider() {
    29.         GameObject cubeGO = new GameObject();
    30.         cubeGO.transform.position = Vector3.zero;
    31.         cubeGO.AddComponent<BoxCollider>();
    32.         return cubeGO;
    33.     }
    34.  
    35.     void BatchOverlapSphere() {
    36.         var commands = new NativeArray<OverlapSphereCommand>(1, Allocator.TempJob);
    37.         var results = new NativeArray<ColliderHit>(3, Allocator.TempJob);
    38.  
    39.         commands[0] = new OverlapSphereCommand(Vector3.zero, 10f, QueryParameters.Default);
    40.  
    41.         OverlapSphereCommand.ScheduleBatch(commands, results, 1, 3).Complete();
    42.  
    43.         int hitCount = 0;
    44.         foreach (var hit in results) {
    45.             if (hit.collider == null) {
    46.                 break;
    47.             }
    48.  
    49.             hitCount++;
    50.             //Debug.Log(hit.collider.name);
    51.         }
    52.         Debug.Log($"HitCount: {hitCount}");
    53.        
    54.         commands.Dispose();
    55.         results.Dispose();
    56.     }
    57. }

    I have sent a bug report: IN-37645 - Crash using OverlapSphereCommand and add/remove gameobjects
     
  2. tatien

    tatien

    Joined:
    Jun 24, 2014
    Posts:
    5