Creating object and collections at runtime

Hey guys,

I’m currently working on a game targeting android and ios platforms. I am quite familiar with object pooling and I make sure to instantiate any GameObjects I’m going to create in OnStart() and then cache/reuse them in the scene.

Should I be concerned with newing regular C# objects at runtime as well? Is this something I should avoid doing?

Currently in my game I might do something like this to create a set of badguys:

var badGuys = new List<BadGuy>();

for(int i=0; i<BAD_GUY_COUNT; i++)
   badGuys.Add(new BadGuy() { hp = 10, attack = 4 });

Will this cause performance issues? Should I be creating objects like this at scene start just like any prefabs/GameObjects I need?

Thanks for any feedback on this!

any allocations could be the cause of memory fragmentation, garbage collection issues, etc. but what you’re doing is quite normal.

cache important items and things that are known to be slow (GetComponent() for example) in Start()/Awake() but for the most part, try not to fall into the premature optimization trap.

be concerned about performance once you’ve got everything working and then profile the hell out of it…

this is a similar question to this: Declaring/modifying lists at runtime - Unity Answers