# RaycastNonAlloc

> Cast a ray through the Scene and store the hits into the buffer.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.PhysicsModule

## RaycastNonAlloc(Ray, RaycastHit\[], float, int, QueryTriggerInteraction)

Cast a ray through the Scene and store the hits into the buffer.

```csharp
public static int RaycastNonAlloc(Ray ray, RaycastHit[] results, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### Parameters

**** (\[Ray]\(/engine/6000.0/script-reference/unityengine/ray)): The starting point and direction of the ray.**** (\[RaycastHit\[]]\(/engine/6000.0/script-reference/unityengine/raycasthit)): The buffer to store the hits into.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The max distance the rayhit is allowed to be from the start of the ray.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A Layer mask that is used to selectively filter which colliders are considered when casting a ray.**** (\[QueryTriggerInteraction]\(/engine/6000.0/script-reference/unityengine/querytriggerinteraction)): Specifies whether this query should hit Triggers.

### Returns

| Type                                                       | Description                                        |
| ---------------------------------------------------------- | -------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The amount of hits stored into the results buffer. |

### Remarks

Like [Physics.RaycastAll](/engine/6000.0/script-reference/unityengine/physics/raycastall.md), but generates no garbage.

The raycast query ends when there are no more hits and/or the results buffer is full. The order of the results is undefined. When a full buffer is returned it is not guaranteed that the results are the closest hits and the length of the buffer is returned. If a null buffer is passed in, no results are returned and no errors or exceptions are thrown.

## RaycastNonAlloc(Vector3, Vector3, RaycastHit\[], float, int, QueryTriggerInteraction)

Cast a ray through the Scene and store the hits into the buffer.

```csharp
public static int RaycastNonAlloc(Vector3 origin, Vector3 direction, RaycastHit[] results, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction)
```

### Parameters

**** (\[Vector3]\(/engine/6000.0/script-reference/unityengine/vector3)): The starting point and direction of the ray.**** (\[Vector3]\(/engine/6000.0/script-reference/unityengine/vector3)): The direction of the ray.**** (\[RaycastHit\[]]\(/engine/6000.0/script-reference/unityengine/raycasthit)): The buffer to store the hits into.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The max distance the rayhit is allowed to be from the start of the ray.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A Layer mask that is used to selectively filter which colliders are considered when casting a ray.**** (\[QueryTriggerInteraction]\(/engine/6000.0/script-reference/unityengine/querytriggerinteraction)): Specifies whether this query should hit Triggers.

### Returns

| Type                                                       | Description                                        |
| ---------------------------------------------------------- | -------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The amount of hits stored into the results buffer. |

### Examples

```csharp
using UnityEngine;

public class ExampleClass : MonoBehaviour
{
    // The size of the array determines how many raycasts will occur
    RaycastHit[] m_Results = new RaycastHit[5];

    // See https://docs.unity.com/engine/6000.0/manual/scripting/object-oriented-development/managing-update-order/execution-order for information on FixedUpdate() and Update() related to physics queries
    void FixedUpdate()
    {
        // Set the layer mask to all layers
        var layerMask = ~0;

        int hits = Physics.RaycastNonAlloc(transform.position, transform.forward, m_Results, Mathf.Infinity, layerMask);
        for (int i = 0; i < hits; i++)
        {
            Debug.Log("Hit " + m_Results[i].collider.gameObject.name);
        }
        if (hits == 0)
        {
            Debug.Log("Did not hit");
        }
    }
}
```
