Foreach loop and Raycast array issue

Hi everyone,

I have been trying to understand this issue for a while and cannot get around it. All of the following is happening inside the Update() method. I made a Raycast array and Raycasthit array for my enemies to be triggered by the player (perhaps I can sweep a single Raycast and that would be more accurate and efficient, but that’s not the point here, I want to make it work my way now). When any Raycast is triggered, the foreach loop is entered to check what each single ray actually hit. For some unknown reason, only two raycasts per frame are actually being used inside the foreach loop, instead of the full set of five rays. Does anyone understand why this should happen?

       RaycastHit[] colliderColpito = new RaycastHit[5]; //https://answers.unity.com/questions/897639/raycast-conundrum.html
        bool[] ray = new bool[5];

        ray[0] = Physics.Raycast(rifTransform.position + transform.up, exNorm0, out colliderColpito[0], distanzaAvvistamento);   //genera raycast e applica filtro layer per ignorare triggers e altri colliders
        ray[1] = Physics.Raycast(rifTransform.position + transform.up, exNorm1, out colliderColpito[1], distanzaAvvistamento);
        ray[2] = Physics.Raycast(rifTransform.position + transform.up, exNorm2, out colliderColpito[2], distanzaAvvistamento);
        ray[3] = Physics.Raycast(rifTransform.position + transform.up, exNorm3, out colliderColpito[3], distanzaAvvistamento);
        ray[4] = Physics.Raycast(rifTransform.position + transform.up, exNorm4, out colliderColpito[4], distanzaAvvistamento);

        //Debug.Log("a" + ray[0] + "b" + ray[1] + "c" + ray[2] + "d" + ray[3] + "e" + ray[4]);

        foreach (bool rayelement in ray)
        {
            Debug.Log(System.Array.IndexOf(ray, rayelement)); //This shows that only a couple of elements of the ray per frame are actually being iterated
        }

Thanks in advance!

I think you are expecting Line 15 does something other than what it really does. At run time, does your foreach loop print out two different numbers, for a total of five lines of output? That’s because IndexOf only shows you the index of the first element of the array ray that contains the value in rayelement. There can only be two values, true and false, so there can only be two entries in ray that are the first ones to contain those values.

Replace Lines 13-16 with this and see what you get:

for (int i=0; i < ray.Length; ++i)
{
    Debug.LogFormat("ray[{0}] is {1}", i, ray[i]);
}

Thanks for the reply, today I will try your suggested code.
Then, does this mean that I cannot use a foreach loop to iterate through a boolean array? Because apart from the System.Array.IndexOf() method, whatever code I use inside the foreach loop, only two rays are being checked. So it seems to be an issue with the foreach loop behaviour mainly.

You can use a foreach, but try it this way:

int i = 0;

foreach(bool rayElement in ray)
{
    Debug.LogFormat("Element #{0} in ray is {1}", i, rayElement);

    ++i;
}
1 Like

Yes, it shows all iterations correctly now and the reason why my other functions inside the foreach loop (not shown in this thread for clarity) were not working was due to the fact that I was using System.Array.IndexOf() also in other parts of the loop.

Thank you for the help!

1 Like