Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by arturo20 · Nov 16, 2012 at 08:22 AM · c#optimizationloops

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Michael Covert · Nov 16, 2012 at 11:39 PM 0
Share

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.

avatar image arturo20 · Nov 16, 2012 at 11:47 PM 0
Share

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.

avatar image arturo20 · Nov 17, 2012 at 12:03 AM 0
Share

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. :(

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arturo20 · Nov 16, 2012 at 08:53 PM 0
Share

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.

avatar image aldonaletto · Nov 16, 2012 at 09:29 PM 0
Share

How complex are the cubes?

avatar image arturo20 · Nov 16, 2012 at 10:13 PM 0
Share

Simple 6 sided cube, no texture, no shaders, no casting or receiving shadows. They meassure 0.4f per side.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image arturo20 · Nov 16, 2012 at 10:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges