Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved 2D - How do I optimize having up to 10,000 colliders in one scene at the same time in

Discussion in 'Physics' started by Sammyueru1, Oct 25, 2021.

  1. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    I need help with optimizing colliders in Unity 2D 2020.3.18f1. I'm using up to 10,000 colliders at a time but the problem is is that whenever the scene loads (Because they all load when the scene loads) Unity or (if built) the simulation itself, stops responding. And I can't seem to get it to work no matter what I try.

    If you know a fix or need more information please just reply.
    Thanks in advance.
     
  2. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    What platform, what kind of colliders (static? Rigidbody?), and what else is in the scene? I was running a testbed the other day with 11,000 colliders and it didn't struggle in the slightest.
     
    Sammyueru1 likes this.
  3. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    The platform really needs to be everything from mobile to pc/mac but the the objects will colliders do use rigidbodies and the colliders are triggers and there are scripts attached to all of the (up to) 10,000 objects with their colliders but even without the scripts they don't work
     
  4. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    Can you try turning them on over time rather than all at once? Not as a fix, but to make sure that's actually what the problem is.
     
  5. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    I'll try that but should I use
    Code (CSharp):
    1. yield return new WaitForSeconds(0.05);
    or something else?
     
  6. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    I was thinking more about switching on the game objects over time, rather than just the colliders.

    EDIT: If the project isn't too big, I can take a look if you like.
     
  7. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    Well it clones a randomly selected prefab with it's collider
    But even with doing a wait it wouldn't work even as a 0.1 second wait
     
  8. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    Are you waiting for 0.1 seconds and then doing them all? Or are you cloning them one at a time, 0.1 seconds apart?
     
  9. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    apart
     
  10. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    Hmm. Then 'colliders' certainly aren't the problem; something else must be locking it up. Something on the things you're spawning, maybe, or something else entirely.

    I'd be happy to take a look if you want to zip it up and DM me a Drive link or something. I'm pretty good at finding bugs - you have to be when you write as many as I do.
     
    Sammyueru1 likes this.
  11. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    Alright I just DM'd you using the conversation thing with the google drive link

    Edit: And thank you so much!
     
  12. Sammyueru1

    Sammyueru1

    Joined:
    Jul 20, 2020
    Posts:
    45
    Alright so Peeling helped me fix it the solution was to do a
    Code (CSharp):
    1. yield return WaitForSeconds(0.005);
    and put the for loop into a IEnumerator
     
  13. URocks

    URocks

    Joined:
    May 1, 2014
    Posts:
    159
    You can try also this approach:
     
  14. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    401
    That wasn't the fix :) That stopped it locking up, but the real cause of it locking up was that you were spawning all 10000 colliders in a tiny area, resulting in 100,000,000 collisions being processed every frame.
     
    MelvMay likes this.
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Gulp!

    It's great that you got help but also, it sounds like the profiler should be used here too; a valuable skill to learn for sure. It would be pretty clear that the number of contacts being handled was the problem. At least if you were to reduce the number of objects so Unity would respond. :)
     
    hippocoder likes this.