I’m going to give a very basic example here, but I really need to know this before I move forward professionally.
Design 1:
A character instantiates bullet prefabs and in a master “Game” GameObject, there is a script containing lists of projectiles that all projectiles are added to. There is a for loop that iterates over the array and tells every projectile to move forward, and after traveling for a few seconds they are destroyed. Something like…
GameObject tempVar = Destroy(Instantiate(prefab_reference), 5.0f);
game_projectiles.add(tempVar);
…
foreach(GameObject projectile in game_projectiles)
{
if(projectile == null)
{
game_projectiles.remove(projectiles); // I know that might not work on an iteration object, but you get the idea
}
projectile.transform.position += projectile.transform.forward;
}
Design 2:
A player instantiates a bullet prefab where each prefab contains a script that will be moving it forward for some duration and then remove it from the scene.
Please someone tell me which is better. Is it best to have each GameObject controlled by it’s own script (like 100 bullets running bullet scripts) or have all game objects added to lists to be managed by a master script (like 100 bullets added to a list and controlled by a for loop) ? I would like to know which is best for unity performance wise. It seems obvious to me with my experience so far that the fewer scripts running the better, but maybe there’s some background thread manager that handles better if each prefab handles itself instead of having minor pauses from moving every prefab in a loop that covers them all?