Glitch Garden AttackerSpawner Array question

Based on the player’s difficulty choice, or as levels increase, I want to change the array of attackers that can be spawned. For ‘Easy’ difficulty only Lizards spawn. For ‘medium’ level lizards and foxes spawn, and so on. The only way I know of populating an array is through the Inspector fields.

Can I declare and populate an array by code?

One way to do this would be to have an instance of each enemy type initialized within the class that handles spawning. Something along the lines of:

public Enemy lizard;
public Enemy fox;

That will expose those parameters to the inspector, so you can just drop your prefabs in. From there, one solution would be to initialize each level in it’s own array:

private Enemy [] easyDifficulty = {lizard};
private Enemy [] mediumDifficulty = {lizard, fox};

You can then have a “currentDifficulty” array that you handle spawning from, and change its value whenever the difficulty changes.

// Initial assignment to default
private Enemy [] currentDifficulty = mediumDifficulty;

// Potential method for changing difficulty
public void ChangeDifficulty (int difficulty) {
    switch (difficulty) {
        case 1:
            currentDifficulty = easyDifficulty;
            break;
        case 2:
            currentDifficulty = mediumDifficulty;
            break;
    }
}
1 Like

Hi @J_Happ,

Did @Gerald_Burke’s answer help you?


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms