Hello,
I have large objects with different types of child components. When certain events happen, certain types of the children need to be modified.
Is it better to “get” and store all of the different types of children in the beginning or “get” them every time a script is run?
Example of storing children in the beginning, then accessing them later:
public A[] allA;
public B[] allB;
public C[] allC;
void Start(){
allA=GetComponentsInChildren<A>();
allB=GetComponentsInChildren<B>();
allC=GetComponentsInChildren<C>();
}
public void doStuffWithA(){
foreach(A a in allA){ a.doStuff(); }
}
public void doStuffWithB(){
foreach(B b in allB){ b.doStuff(); }
}
public void doStuffWithC(){
foreach(C c in allC){ c.doStuff(); }
}
Example of “getting” children every time it does stuff:
public void doStuffWithA(){
A[] allA=GetComponentsInChildren<A>();
foreach(A a in allA){ a.doStuff(); }
}
public void doStuffWithB(){
B[] allB=GetComponentsInChildren<B>();
foreach(B b in allB){ b.doStuff(); }
}
public void doStuffWithC(){
allC=GetComponentsInChildren<C>();
foreach(C c in allC){ c.doStuff(); }
}
Per-tick lag is an issue in this game. Graphics and memory are less so.
These scripts are run every 5 seconds on average.
Thanks,
Nomenokes