From what I understand, you use get_first_unused_layer in data, but don't add it to any collision mask. The way that function works is that it checks all the prototypes in the game, and checks if they have it. Since you didn't use it for any collision mask, it didn't get reserved properly.
Either add it to something, ANTHING, right away, or move that entire logic to the same stage where you actually apply it.