Raycast collides on nothing?

Hi, I’ve googled pretty hard for anyone with my problem, but to no avail. So here goes:

My raycast collides on seemingly nothing. Raycasthit.distance returns “42.62…” and Raycasthit.collider returns “null”.

{
    RaycastHit hit;
    Physics.Raycast(transform.position, direction, out hit);
    
    Debug.DrawRay(transform.position, direction * hit.distance, Color.green, 4f, true);
}

I’ve also tried increasing the raycast distance “manually” (even though that wouldn’t make sense), with

Physics.Raycast(transform.position, direction, out hit, mathf.infinity);

So yeah, what is going on?

Thanks for your time!

EDIT:
It seems like I provided too little code. I’ve figured out that it apparently is relevant to mention that the raycast is made inside of a for loop:

for(int i =0; i < x; i++)
{
   for (int j = 0; j < y; j++)
   {
        RaycastHit hit;
	Physics.Raycast(transform.position, direction[i,j], out hit);
   }
}

hit.distance works properly (i.e. is able to become infinitely large) as long as hit.distance never has been smaller earlier in the loop.

Example: Physics.Raycast(transform.position, direction[0,0], out hit) is always able to return infinty, whereas
Physics.Raycast(transform.position, direction[x-1,y-1], out hit) almost never is (since the raycast is bound to hit something until then).

The contents of your RaycastHit are only valid if Physics.Raycast returned true (indicating that it hit something).

There may be some data present, regardless, but it’s garbage if the raycast didn’t actually hit anything.

Usual structure for a raycast is more like this:

RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit)) {
	//hit something!
	Debug.DrawRay(transform.position, direction * hit.distance, Color.green, 4f, true);
} else {
	//didn't hit anything
	Debug.DrawRay(transform.position, direction * 1000f, Color.red, 0.1f, true);
}

I managed to find a solution by resetting hit.distance = 1000f; after reading it.

Doesn’t make a lot of sense to me but at least it works…

Although this is late, and this is an unorthodox solution, disable “queries hit triggers” in the physics menu, under “Edit → Project Settings”