- Home /
Question by
berrangejanko · Mar 14, 2021 at 10:00 AM ·
loopforeachtrailrendererloops
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;
}
}
'''
Comment
Best Answer
Answer by berrangejanko · Mar 14, 2021 at 10:23 AM
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[i].emitting = true;
}