- Home /
Calling all instances of script at once
I have procedural generated level, assembled from clones of 1 tile prefab which has a script that defines features of prefab like sprite, collider, position, neighbor tiles etc. all those features are updated each frame and changed accordingly to current situation of level.
My question is: how can I make it more efficient, I want to call all instances to check/adjust settings not each frame but only when one of game objects changes it's state so every other object can adjust to new changes.
Here's what I did: I use
`public delegate void CustomUpdate ();
public static event CustomUpdate cUpdate;`
and call cUpdate every time something happens that every tile has to change to adapt to new situation
so in tile's script
`void OnEnable()
{
Generator.cUpdate += setSprite;
}
void OnDisable()
{
Generator.cUpdate -= setSprite;
}`
It works, but I feel like it's work around, maybe there is other way to call every instance of prefab at once ? And can I build every interaction between script who doesn't have direct reference by using delegate/event ? or it's less efficient than using List of all initialized objects and running foreach loop with getComponent to access script of each individual tile and run that update method ? Because this sounds really inefficient
Answer by rageingnonsense · Feb 21, 2015 at 09:41 PM
You could do a hybrid approach. You could have a class attached to an empty gameobject that keeps track of state. for instance, this could be it's update function:
public objectsChanged = false;
void update() {
if(objectChanged == true) {
foreach(GameObject go in internalListOfGameObjects) {
// do stuff...
}
}
objectChanged = false;
}
Have an event set up for each object that this script will pick up. like OnChange(). This woudl set objectChanged = true. This way you only run this loop when required.
Your answer

Follow this Question
Related Questions
Prefab with UI.Text is not unloaded 1 Answer
Hundreds of weapons, best practice 0 Answers
Multiple instances of an Particle System prefab have the exact same animation. 1 Answer
About Model Combine Optimization? 1 Answer
Huge prefab file sizes 0 Answers