Instantiate a prefab and clone a cube in the prefab hundreds of times = 5 draw calls. Do the same thing again = thousands of draw calls?

Hey!

I have a GameObject prefab (Empty except for some scripts) that has a single child object (a cube, also a prefab).

I can instantiate the main prefab in my level and then instantiate the cube hundreds of times and I get 5 draw calls.

However, if I repeat the same exact thing, I end up getting thousands of drawcalls (one for each cube). Any ideas?

      GameObject tempCubeset = ( GameObject ) Instantiate( cubeSetPrefab , new Vector3( 0.5f , -11.5f , 0.5f ) , Quaternion.Euler( 0 , 0 , 0 ) );
      tempCubeset.transform.GetChild( 0 ).gameObject.renderer.material = new Material( Shader.Find( "Transparent/Diffuse" ) );

      for ( int i = 0 ; i < 500 ; i++ )
      {
          tempCubeset.GetComponent<CubeSet>().AddCube( new Vector3(0.5f, -11.5f, i+1.5f) );
      }


      //repeat (this forces a draw call per cube?)
      tempCubeset = ( GameObject ) Instantiate( cubeSetPrefab , new Vector3( 2.5f , -11.5f , 0.5f ) , Quaternion.Euler( 0 , 0 , 0 ) );
      tempCubeset.transform.GetChild( 0 ).gameObject.renderer.material = new Material( Shader.Find( "Transparent/Diffuse" ) );

      for ( int i = 0 ; i < 500 ; i++ )
      {
          tempCubeset.GetComponent<CubeSet>().AddCube( new Vector3( 2.5f , -11.5f , i + 1.5f ) );
      }

Any ideas what is causing this?

For context: I want to dynamically create “layers” of these cubeSets and be able to have several cubeSets each containing several cube children of their own. I am looking for a way to change the material/color of all cubes inside of a given cubeSet at runtime without having to step through every child individually. I am trying to use renderer.sharedMaterial to do this which seems to work great with one cubeSet, but not two?

I just figured it out.

It was being caused by the Shader “Transparent/Diffuse”. When I created one cubeSet there was no overlap so things were fine. Adding the second just two units higher caused the transparency in the cubes to give me grief.

I’m guessing that a transparent cube needs its own draw call if there is a material behind it (at least it makes sense to me). Changing over to a plain “Diffuse” fixed my draw call problem.