I have 5 particle systems that i would like to change with a for loop. They all have similar names except the number at the end.
public BoostManager particle1;
public BoostManager particle2;
public BoostManager particle3;
public BoostManager particle4;
public BoostManager particle5;
for (int i = 1, i < 6, i++)
{
particle + i.ActivateBoost();
}
Gah! That link starts out with the motivating example (having variables likes Number1, Number2 … ) and says arrays can make this work better. That’s good. But then it devolves into every horrible textbook by listing and describing every tiny little rule.
Arrays were invented to solve that exact problem – a list of same-type variables that you can find by #:
public BoostManager[] particles;
// user will click in Inspector, enter 5 and get slots 0-4
// sample use:
// particles[0].ActivateBoost();
// do to all:
for(int i=0; i<5; i++) // basic counting for-loop: 0 to 4 is 5 things
particles[i].ActivateBoost();
Unity makes public arrays a little simpler to code, since you give them a size using the Inspector, and also fill them by dragging and/or typing there. That’s why all we need is that 1st line saying particles is an array of BoostManagers. We don’t need to give it a size of 5 in code.