C# - foreach not working!

Hello Everybody - i have this script to return all the names from the hits. But its just wont work!!

 Hits = Physics.RaycastAll(transform.position, transform.forward, 10000f);
        foreach (RaycastHit hit in Hits)
        {
            Debug.Log(hit.transform.gameObject.name);
        }

I dont know what i did wrong every debug.log outiside the foreach is working!!

Thanks in Advance!

Well, if your debug,Log inside the foreach loop isn’t executed, that just means your raycast didn’t hit anything and the Hits array is empty. The foreach loop does one iteration for each element in the array / collection. If there is no element in the array, the loop won’t execute.

Try printing the array length before your loop:

Debug.Log("Hit count: " + Hits.Length);

If it prints “Hit count: 0” you know that you simply don’t hit any collider.