Foreach loop not going through all the children

Im trying to loop through an array of Trail Renders (length: 2), but it does not loop and only runs once, so only 1 trail render gets enabled…
In c#, unity 3D

Code:


    TrailRenderer[] AllTrails;
    
    private void Start()
    {
         AllTrails = DriftingParticels.GetComponentsInChildren<TrailRenderer>();
    }
    

    public void Update()
        {
            GetInput();
            HandelMotors();
            HandelSteering();
            UpdateWeels();
            CheckDrifting();
            DriftOrNot();
        }
    
    private void DriftOrNot()
    {
        foreach (TrailRenderer child in AllTrails)
            {
                 child.emitting = true;
            }
     }

Soloution: Use a for loop instead of an foreach loop:

Replace

foreach (TrailRenderer child in AllTrails)
{
   child.emitting = true;
}

to:

for (int i = 0; i <= AllTrails.Length - 1; i++)
            {
                AllTrails*.emitting = true;*

}

My issue was that I actually used a for loop just like in the answer above but I destroyed the objects within the loop. This meant that only every second object would be processed since the index gets increased and the collection shrinks with every iteration.

The solution was this:

for (transform.childCount > 0) { Destroy(transform.GetChild(0).gameObject); }