Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


OverlapSphereNonAlloc(Vector3, float, Collider[], int, QueryTriggerInteraction)

Computes and stores colliders touching or inside the sphere into the provided buffer.
Read time 1 minuteLast updated 5 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.PhysicsModule
public static int OverlapSphereNonAlloc(Vector3 position, float radius, Collider[] results, int layerMask, QueryTriggerInteraction queryTriggerInteraction)

Parameters

position

Center of the sphere.

radius

Radius of the sphere.

results

The buffer to store the results into.

layerMask

A Layer mask defines which layers of colliders to include in the query.

queryTriggerInteraction

Specifies whether this query should hit Triggers.

Returns

Type

Description

intReturns the amount of colliders stored into the results buffer.

Remarks

Does not attempt to grow the buffer if it runs out of space. The length of the buffer is returned when the buffer is full. Like Physics.OverlapSphere, but generates no garbage. Additional Resources: Physics.AllLayers.
using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour{ void ExplosionDamage(Vector3 center, float radius) { int maxColliders = 10; Collider[] hitColliders = new Collider[maxColliders]; int numColliders = Physics.OverlapSphereNonAlloc(center, radius, hitColliders); for (int i = 0; i < numColliders; i++) { hitColliders[i].SendMessage("AddDamage"); } }}
Additional Resources: Ray cast with layers section of Use of layers in Unity.