Question by
Owenja147 · Dec 06, 2016 at 07:01 PM ·
c#arrayscripting beginner
How do I use an entire array at once?
I have an array of collides. I want to know how to use the entire array at once instead of typing the same thing all the time for the different collides in the array. For example:
//using c-sharp
Public Collider [] colliders;
void FixedUpdate ()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.forward, out hit, 40))
{
if (hit.collider == colliders [0])
//some code...
}
}
Instead of writing "if (hit.collider == colliders [0] || hit.collider == collider [1] || ...)" is there a way to type the array once and it applies to every variable inside the array? Or is there a better method than using Arrays?? (I'm new to development and scripting in general)
Comment
Best Answer
Answer by Aklen · Dec 06, 2016 at 09:40 PM
Why not just use a foreach/for loop?
foreach(var colliderObj in colliders){
if (hit.collider == colliderObj){
//some code...
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612102159im_/https://answers.unity.com/themes/thub/images/avi.jpg)