Instantiating and draw calls with lots of objects

I don’t understand Instantiating.

When I do this I get few draw calls.

            if (i < 10) {
                GameObject temp = (GameObject)Instantiate (sprite1, transform.position, transform.rotation);
            } else if (i < 20) {
                GameObject temp = (GameObject)Instantiate (sprite2, transform.position, transform.rotation);
            } else if (i < 30) {
                GameObject temp = (GameObject)Instantiate (sprite3, transform.position, transform.rotation);
            } else {
                GameObject temp = (GameObject)Instantiate (sprite4, transform.position, transform.rotation);
            }

But when I do this I get a lot of draw calls.

            if (UnityEngine.Random.value < 0.25) {
                GameObject temp = (GameObject)Instantiate (sprite1, transform.position, transform.rotation);
            } else if (UnityEngine.Random.value < 0.25) {
                GameObject temp = (GameObject)Instantiate (sprite2, transform.position, transform.rotation);
            } else if UnityEngine.Random.value < 0.25) {
                GameObject temp = (GameObject)Instantiate (sprite3, transform.position, transform.rotation);
            } else {
                GameObject temp = (GameObject)Instantiate (sprite4, transform.position, transform.rotation);
            }

The difference is only the random choice of Instantiate.

I am trying to generate a List<> of Creatures and then attach prefabs to each one. I am reusing some prefabs and I don’t want extra draw calls, but all my creature prefabs are randomly selected.

So far when I generate 40 creatures I get either 13 draw calls or 30+ depending on which of the above I use but I just don’t understand why.

The rest of the script is like this

public class GameData : MonoBehaviour
{
    public List<Creature> Creatures = new List<Creature> ();
    public void Start ()
   {
     int creaturelimit = 40;
     for (int i = 0; i < creaturelimit; i++) {
       Creatures.Add (new Creature (){ });
       // Instantiates go here
   }
}

The two bits of code produce very different distributions. In the first one you’ll get exactly 10 of sprite1 then 10 of sprite2 and so on. The second one will produce randomly produce approximately 25% of sprite1, 18.75% of sprite2 ,14.0625% of sprite3 and 42.1875% of sprite4

Are sprite1, sprite2 sprite3 and sprite4 all the same complexity and do they all batch correctly? If sprite4 is more complex (or doesn’t batch as well) as the others then the fact that there are way more of them would explain the difference in draw calls.

they are all similar. 2d 128 pixel prefab with transparency. its as if it only sees a match when previous is the same. maybe cant batch with transparency when previous isnt the same and packs in a new one.

Have they been combined into a single texture, possibly using the Sprite Packer / setting the Packing Tag?

I’ve not see that setting yet. No they are just 4 prefabs using different textures set up in the editor. I assumed it handled that itself since the draw calls are minimal if they are all loaded sequentially.

Note: I can’t access the Sprite Packer window, but my Sprite Pack setting is Always Enabled. Maybe the free version doesn’t do it properly?

At the moment I will just stick to setting up all my sprites sequentially before doing anything random.

Tested transparency, no difference.