- Home /
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?
Sure, the AddCube...
public void AddCube( Vector3 cubeCoord )
{
GameObject cubeSection = (GameObject)Instantiate(baseCube, cubeCoord, Quaternion.Euler(0, 0, 0));
cubeSection.transform.parent = transform;
}
Edit: the parent transform pushes the cube into the empty cubeSet object.
Answer by DevLurkin · Oct 04, 2014 at 10:27 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164410im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers
How to randomly instantiate other prefabs parallel? 1 Answer
What can prevent a prefab network instantiation? 0 Answers
my object keeps cloning is self i want it to clone once 2 Answers
Instantiate PreFab with C# Script 5 Answers