Loading 2D Tilemaps prefabs

I am making a 2d game with tilemaps. I'm thinking of having a large world that is always loaded, but not always visible (there should be npc:s in different part of the world that can interract with other objects). As I have understood it by having many tilemap objects unity will cull out those not visible.

So I have prefab objects that I call zones, they consist of tilemap chunks. One zone is nine chunks, each chunk is a 32x32 tilemap. I load these prefabs and then instantiate them roughly like this:

Ocean = (GameObject)Resources.Load("Prefab/Zones/ZoneOcean"); // This is only loaded once

GameObject tmp_Ocean = Instantiate(Ocean);
m_Zones[×][y].parent = tmp_Grid;
} // this is run for each zone to "generate"

Right now the zones are instantiated in the Start() function.

The problem I have is that instantiating a few of these prefabs crashes unity when running it. The problem seems to be memory related since the memory usage keeps increasing until the crash occures.

How can I instantiate large prefabs during the start of the program? Should I make some kind of load function instead of doing this in the Start()? Can this be solved by instantiating them in a coroutine queue? Or will I always have this problem when trying to instantiate the objects in runtime?


If your chunk is 32 x 32 it is1024 tiles - and if your one "zone" is 3x3, you will have 9216 tiles in one zone.

So how many zones of this size you have, and how large textures you are using? Maybe check what your profiler says. And how much memory you have in your system?

"The problem seems to be memory related since the memory usage keeps increasing until the crash occures."

In general, if you fill your computers memory you'll run into problems... not much you can do other than not fill your memory. Yet I have no idea if your problem has anything to do with tile maps causing the crash, it might be as well something else.

You said you are trying to split your world into chunks - so why are you trying to load all the chunks at once? This makes no sense. You could maybe create some sort of system that loads new level chunk when you get close to edge of current chunk, and then unload the previous one when you cross some area in your new chunk.

Culling does not remove the object from memory, so all those chunks are still going to take up space in your memory even when they are not on screen.

To clarify. I can have 9 zones in the game, if and only if they are created during compiletime. The problem is creating these zones during run time, not the existance of the zones themselves. As to why they are chunks is because different chunks within the zones will have different properties.

Yet you didn't address the issue of you generating everything at once...

Could you file a bug using the Unity Bug Reporter with your project? If there is a memory issue, we would need to fix it within the Unity Editor.