# Using coroutines to instantiate larger GameObjects in Unity/C#

We are making a scroller type game for mobile platforms and we got pretty far (Beta is already out). Now we are adding some additional content and are starting to experience a noticable lag when starting the game. I know about coroutines, but never actually used them, so I was wandering if it's possible to use them to instantiate larger GameObjects (or groups of objects). This is the code we use at the moment to create bullets (this is just an example, I have more similar methods):

Func<string, Pool.Poolable> bulletCreator = x => { return Instantiate(Resources.Load<Pool.Poolable>(x)) as Pool.Poolable; };


and then we use this method to create a certain amount of bullets:

Bullets = new Pool(4, () => { return bulletCreator("Bullet"); });


This creates 4 bullets (it fits our game desing, the player is restricted to 4 bullets and can refill them with powerups). There are different types of bullets that we create with the same function, and we create them at the start, pool them, and then use them when they are needed.

Now this is one of the "heavier" methods, and I'm trying to find a way to speed it up. I tried something like this:

  IEnumerator CreateBullets()
{
Func<string, Pool.Poolable> bulletCreator = x => { return Instantiate(Resources.Load<Pool.Poolable>(x)) as Pool.Poolable; };
Bullets = new Pool(4, () => { return bulletCreator("Bullet"); });
yield return Bullets;
}


Bullets is a global private variable of type Pool. The problem we are having is that when we try to shoot, Unity trhows the Object not set to an instance error. Is what I'm trying possible, or is it like in the regular C# that no crossthread calls are allowed. If it's not possible, what are our options? I know we could probably make a short animation and run that in the coroutine while the main thread instantiates everything. Any advice is very welcome.

• I take it the "Poolable" class is contained within the "Pool" class? Is this class attached to a GameObject? Also ensure the prefabs you wish to load are inside of a folder named "Resources" inside of the "Assets" folder... Otherwise it won't be able to find the object. Aug 29 '15 at 22:22
• Poolable is just a small abstract class inside the Pool class with a couple of parameters that we use to trigger or clean up the objects. Pool is a public class, it is not directly attached to any GameObject/Prefab (it doesn't even inherit MonoDevelop). All of my resources are in the "Resources" folder - like I said the code that we currently use works fine, it is just slow. Aug 30 '15 at 8:20
• Ok... This doesn't sound like it works fine "The problem we are having is that when we try to shoot, Unity trhows the Object not set to an instance error." But fair enough... Good luck. Aug 30 '15 at 8:52
• Oh, sorry, maybe I didn't form the question right - the problem appears when we use the coroutine to create the bullets, while it is in the original thread it works just fine. So the first two lines are placed in the Awake() method of a script that is attached to the character GameObject. They work perfectly well until I tried to place them into the coroutine, then the error appeared. Aug 30 '15 at 9:28

I have faced similar problem before. I am pretty sure that co routines won't help you reduce the lag. Multi threading will be a proper solution instead of co routines.

But apparently I didn't use either of them. My game had finite predefined pool objects. So instead of instantiating them, in the editor I made a parent game object named Pool and made all the bullets as children. All I did was, on Awake()

Bullet [] bullets = GetComponentsInChildren<Bullet>();


And referenced all the poolable objects into the array without any lag :)

• Yeah, I think you are right, I managed to get the code working by using StartCoroutine(Name), and it works, but you are right, I don't think it reduced the lag. At the moment we also have a predefined pool of objects, but you have to instantiate them at some point, I am trying to speed that part up. Aug 30 '15 at 9:58
• Can't you make it already available in the hierarchy in the scene itself ?? Is it something need to be done on the run time only ?? Aug 30 '15 at 10:09
• Hm that is a good idea, I could add them directly to hierarchy - but I would have to assign them to the proper variables in the game script later, Would that help the performances? Aug 30 '15 at 10:39
• Thats exactly what I have done and also explained in the answer that there is no lag :) Aug 30 '15 at 13:58