Using i in if statement

So I’m doing some raycasting with 5 raycasts and figured since they all looked alike,
why don’t I use a for loop instead with “i” as a varying factor.

So what I wanna do is something like

void Update(){

     for (int i = 1; i < 6; i++){

            if (Physics.Raycast(T(i), fwd, out hit(i), 5))
            {
                if (hit(i).transform.tag.Contains("Short"))
                    hitShortBar = true;

                if (hit(i).transform.tag.Contains("Tall"))
                    hitTallBar = true;
            }
        }

T being the transform I’m using

T1, T2… and so on
asweel as hit1, hit2… and so on.

now I know this wouldn’t work with the parenthesis around “i” that’s just where I want to use “i” :).

Is this possible?

I think the array is not there, yet. I think what he has is T1, T2, T3 as variables and what is wanted is something like this:

void Update(){
      Transform[] T = { T1, T2, T3, T4, T5, T6 };
      for (int i = 0; i < 6; i++){
             if (Physics.Raycast(T*, fwd, out hit, 5))*

{
if (hit.transform.tag.Contains(“Short”))
hitShortBar = true;
if (hit.transform.tag.Contains(“Tall”))
hitTallBar = true;
}
}
}
There is no need to to define the array in Update() each time, it is just an example. You can probably just add a public Transform[] T; member to your class and fill it in the inspector.

Im not sure what your question is… But yes what you’ve put down should work. Will be heavy on processing tho… maybe put the for loop in a function instead and only call it when you need to.

Assuming that T is an array of GameObjects and hit is an array of RaycastHits, then what you’ve written will nearly work except it requires square bracket syntax to access the index of an array rather than round brackets:

if (Physics.Raycast(T_, fwd, out hit*, 5))*_