Sporadic error bug

Hello! I’ve been getting this error sporadically for months, maybe like once every hundred or two hundred scans and I can’t figure out why it’s happening.

Debug.LogError("Too many layers, a maximum of " + LevelGridNode.MaxLayerCount + " are allowed (required " + numHeightSamples + “)”);

That’s the line showing the error, in the LayerGridGraphGenerator. The max layer count is 255, but sometimes the algorithm gives higher than that but not by much (last time, it was 327 layers). It happens so rarely that I couldn’t find out if it breaks the graph or not. Why does it sometimes give it a higher number of layers? Could it be caused by mountains on the terrain? I assume that if for example you have a mountain with a height of 256 and each layer represents 1 meter, then the layered grid graph would break right?

How can this be fixed? I tried increasing the constant from 255 to 1020 but then the code threw errors in other places in that same class.


The number of layers is the number of hits that the raycast hits when it is fired from the sky down towards the ground. If it says 327 layers, that means that in some part of your world you have a stack of 327 colliders (or at least 327 faces of some colliders intersected the ray). That seems like a lot. Do you have any large stacks of colliders somewhere?
For a normal terrain, regardless of how high it is, with nothing else on it and no other colliders the graph would only require 1 layer as there is only a single intersection point with the ground for any given x,z cell.

Ah okay thanks for the clarification! Well, the world is procedurally generated, except for the terrain heightmap. My best guess is that this occurs from the rock formations I spawn on the map. Basically, each rock formation can have 40-60 rocks (with unique slightly different heights and scales) laid out on the terrain and some of them might intersect with each other (only like 1-2 intersections max). Then on each rock, there’s a chance to spawn a few minerals and sometimes you can see 2 or 3 minerals spawning on top of each other. So in all, there should never be more than ~4 colliders on top of each other. So on top of all this, there might be 4-5 of these rock formations in the bounds of one graph. BUT some rocks and some minerals have mesh colliders, some convex and some not. So my best guess is that the error comes from the non convex colliders stacking on top of each other in weird angles.

I tried understanding the code a bit but I can’t figure out if we’re talking on a cell by cell basis (since the method is called recalculateCell) or if we’re talking about a whole graph. Is the 255 limit imposed on a cell or for the whole graph? If it were for the whole graph, then those rock formations could easily reach 255(4 rock formations times 40 rocks times times 2 minerals). If it were for one cell, then it would have to be due to non convex mesh colliders with too many faces on them.

In any case, I modified the method a bit to call the SampleHeights method again only once in case the bug happens again, but with some debugging messages in there that shows what the raycasthits contain. I’ll get back to you with the results! For now, I’ll just have to deal with it by restarting the game it seems like.

It’s per cell. Since this is the maximum number of layers, each cell can contain at most 255 nodes stacked on top of each other.

Are you sure you really need mesh colliders for all those rocks? You can also adjust the ‘span merge range’ field on the layered grid graph. It will merge together raycast hits that are closer than that distance.

Ah okay thanks, I’ll make sure to try that. Yes, I’ve already went through optimizing colliders and some of them need to be non convex meshes sadly.

1 Like