Disable colliders outside of camera view

Im currently creating a 2D base builder in which the player spawns on an island and has to gather resources to automate stuff, defend the base and so on.

My problem is that if i create a really large map (1000x1000) I get like 12 fps due to the hundreds of colliders of the resources. If i deactivate them everything works perfectly fine. I probably wont need map this large but i want to make my game as efficient as possible.

Now im looking for a way to only enable/disable colliders within/outside the main cameras frustrum. Currently im doing it like that:

void FixedUpdate()
{
    if(gameObject.GetComponentInChildren<SpriteRenderer>().isVisible)
    {
        collider.enabled = true;
        return;
    }
    collider.enabled = false;
}

But as you can imagine its not really better due to hundreds of trees and rocks calling FixedUpdates at the same time.

Any suggestions i could try?

I guess, you’re using TIlemap already for the ground part? And as for the objects, there are a few possible ways to try:

  1. You can spawn objects only in close proximity to the character and despawn objects outside of it (you can use Pool pattern for this to make it even smoother)
  2. To disable FixedUpdate in your scripts, you can disable scripts in which it’s called, just like you did with collider in your example code
  3. Also, looking at your code, make sure you don’t call GetComponent methods every frame, this is heavy, instead, cache required components at Start just once and re-use them inside your Update methods