Turns out I needed to add a wait command in the following code:
Why do you set AllEnemiesDefeated to true immediately at the start of layout?
Deactivating/activating the groups as Igor has suggested is a better solution than adding a wait.
Another good option is to load the array in advance in a separate layout (say, in the splash screen or loader).