You cannot immediately add parent's library to newly generated hidden child collection, since it would allow to get several of those hidden collections with same name/library. That is strictly forbidden! So rather loop again on collections after all hidden ones have been generated, and assign children's library from parent one then.