- Home /
How many objects is too many?
I'm building a game demo where the ground is made up of small cubes that rearrange themselves as the player moves around.
The floor itself has 196 cubes but Unity seems to really struggle to put them on the screen, it takes a really long time for them to load and the frame rate drops to an unbearable crawl (<2fps).
I wonder what could be an efficient way of not only creating them but also handling their logic since their position is changing all the time and if Unity struggles to created them I don't think it could handle that mane calculations every frame.
Here's the code I used to create them, the logic for the cubes is already written but I haven't tested it due to this problem.
//Create cubes
for (int z = 0; z < 14; z++)
{
for (int x = 0; x < 14; x++)
{
cube_transform[x,z] = Instantiate(transform,
new Vector3(RL.x + x_counter, floor, RL.z + z_counter ),
new Quaternion(0,0,0,0)) as Transform;
x_counter+=0.4f;
}
z_counter+=0.4f;
x_counter = 0;
}
return;
Edit: Something happened and Im not sure exactly what. The code is exactly the same as the one posted above but performance seems to have improved dramatically, going from under 2 fps to above 70. So I went ahead and tested my logic code to move the cubes around and...well Unity did not like it any more than the previous code and now it gets absolutely stuck when I hit play.
Am I supposed to be doing this another way?
Here's basically how the code works (the create cubes above happens once at Start()).
void CheckFloor()
{
for (int z = 0; z < 14; z++)
{
for (int x = 0; x < 14; x++)
{
if (NeedsToMove(cube_position[x,z]))
{
MoveCube(cube_position[x,z], FindNewPosition(cube_position[x,z]));
}
}
}
}
NeedsToMove returns a bool and it only checks if the distance from the cube to the center of the player is greater than a set distance. MoveCube takes the cube that needs to be moved and moves it to the position determined by FindNewPosition.
Is this way too much logic to handle per frame?
Are your cubes static? $$anonymous$$oving static objects makes unity barf.
Do they all have colliders/rigidbodies? There might be a lot of cube-on-cube collision going on - try to use collision layers to remove these collisions/collision checks.
They're not static and yes they all have collider's but even if I leave an offset so they're not in contact with each other it still gets stuck. Ill try placing them on different collision layers and see what happens. Ill post the results.
So, after placing the cubes in a separate layer that only collides with the character and leaving an offset in their position so they're not "touching" each other the result is exactly the same. :(
Answer by Paulius-Liekis · Nov 16, 2012 at 10:46 AM
That doesn't make much sense. Unity should not struggle with 200 objects. Make sure your problem not somewhere else.
HI, thank you for your answer but I am sure this is the problem, I went as far as making a blank new scene and wrote code just like the one I posted and attached it to a cube and the same thing happened, Unity acts like it's stuck in an infinite loop.
Simple 6 sided cube, no texture, no shaders, no casting or receiving shadows. They meassure 0.4f per side.
Answer by Hedayk · Nov 16, 2012 at 10:13 PM
Check your draw calls, tris, and verts. Post them if you'd like. I don't use C# so I probably won't be much help, but make sure that you're not endlessly generating cubes through Instantiate.
A better way would probably be to generate a "chunk" of cubes as a prefab instead of one at a time. Instantiating every frame would likely make it slow.
Thats all I can think of that would make this occur. Hope this helps.
Hi, thank you for your reply. The cubes are instantiated only once at Start(). The reason I dont generate them by chunks is that they need to move individually, "creating" floor for the character to run around in. Basically the ones in the back move to the front and from side to side.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Null in GetValidMethodInfo 0 Answers
Trouble with Procedural Mesh Normals (normals are inverted) 1 Answer