Laser Defender - Instantiate multiple enemy types

I’m trying to modify the following so that multiple enemy types spawn in a single wave rather than just one.

I think I need to use an array or list but not sure how to implement this. This is on a ScriptableObject which isn’t attached to a GameObject so not sure how to go about this.

public GameObject GetEnemyPrefab() { return enemyPrefab; }

Can anyone point me in the right direction?

Hi Whirz,

Welcome to our community! :slight_smile:

An array with enemy prefabs is a good idea. What is missing is the logic/pattern how the enemy to be spawned is supposed to be selected. Randomly? Alternating index?

Hi Nina, I think randomly from the array would be fine.

Then create an array, and a method which returns an enemy prefab from that array. You already know a method which generates a random integer. See your Number Wizard UI project.

Your current GetEnemyPrefab method returns the value of the enemyPrefab variable but it could return an element from your enemy prefabs array instead.

Hint:

return enemyPrefabs[index];

Thanks, I’m still a bit confused, this is as far as I got:

    [SerializeField] int max;
    [SerializeField] int min;
    [SerializeField] GameObject enemyPrefabs;
    [SerializeField] GameObject pathPrefab;
    [SerializeField] float timeBetweenSpawns = 0.5f;
    [SerializeField] float spawnRandomFactor = 0.3f;
    [SerializeField] int numberOfEnemies = 5;
    [SerializeField] float moveSpeed = 2f;

    int[] index = { 0, 1, 2, 3 };

    public GameObject[] GetEnemyPrefab() {
        enemyPrefabs = Random.Range(min, max);
        return enemyPrefabs[index];
    }

Does the array need to be of type GameObject?

Well done so far. You are almost there. :slight_smile:

Check the nextStates variable in your Text101 game. This is how you should declare your variable. Instead of State, you would use GameObject.

If you want to know the length of an array, you could use .Length.


    [SerializeField] GameObject[] enemyPrefabs;
    [SerializeField] float timeBetweenSpawns = 0.5f;
    [SerializeField] float spawnRandomFactor = 0.3f;
    [SerializeField] int numberOfEnemies = 5;
    [SerializeField] float moveSpeed = 2f;

    public GameObject GetEnemyPrefab() {
        int index = Random.Range(0, enemyPrefabs.Length);
        return enemyPrefabs[index];
    }
1 Like

Thanks Nina!

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

Privacy & Terms