[C#] help with having objects spawn on any of the spawnpoints at start of game

Hi everyone!

I’ve been struggling with this for the past few days, and I can’t get it to work. I’m currently working on a game in C#, where you have to collect 6 ‘spirits’ (animated flames made out of particles) in order to ‘win’.

My idea was to create around 20 spawnpoints around the map where these flames could spawn, so each of the flames will be in ‘random’ spots all over the map every time you start up the game (so not during the game). I also need every spawnpoint to hold only one of the flames, of course. The idea was also to have them rotate around the spawnpoint, like they are flying. I already have the code for this, however, so that is no issue.

So, in short: I want to spawn 6 objects on any of the 20 spawnpoints, seperately, every time I start up the game using C#.

Thanks in advance guys!

Create an empty GameObject, set its position to the origin (0, 0, 0), and add all spawners as child objects to the empty GameObject. Add a “SpiritSpawner” class to the empty GameObject that has a list of objects to hold your “spirit” prefabs (assuming you are using prefabs) and a list of GameObjects to hold the spawners you added as child objects to the empty GameObject.

Make sure you drag and drop your “spirit” prefabs into the list of objects in the “SpiritSpawner” class you added to the empty GameObject so it knows what to spawn.

Here’s an example of the SpiritSpawner class:

using UnityEngine;
using System.Collections.Generic;

public class SpiritSpawner : MonoBehaviour {

    public List<GameObject> spawners = new List<GameObject>();
    public List<Object> spiritPrefabs = new List<Object>();

    void Start() {
        PopSpawnersList();
        SpawnRandomly();
    }
    void PopSpawnersList() {
        // Adds all child GameObjects (spawners) to spawners list.
        foreach(Transform child in transform) {
            spawners.Add(child.gameObject);
        }
    }
    void SpawnRandomly() {
        List<GameObject> unusedSpawners = spawners;

        // Spawn all spirits in spiritPrefabs list at random
        // unused spawners in unusedSpawners list.
        foreach(Object spiritPrefab in spiritPrefabs) {

            // Create random index to access random spawner that
            // has not already been used to spawn a spirit.
            int spawnIndex = Random.Range(0, unusedSpawners.Count);

            // Instantiate spiritPrefab at the unused spawner's position.
            Instantiate(spiritPrefab, unusedSpawners[spawnIndex].transform.position, Quaternion.identity);

            // Remove spawner at spawnIndex as it has been used to 
            // spawn the current spirit.
            unusedSpawners.Remove(unusedSpawners[spawnIndex]);
        }
    }
}

When the game starts, this class will list all the child objects (spawners) and Instantiate all the “spirit” prefabs you added to the spiritPrefabs list at random spawners that have not already been used to spawn a “spirit”. Test it out and let me know if it works.

Ah, lovely! Just tried it out and it works, so far so good. Thanks a whole bunch! You certainly saved me from getting some more headaches, bless. Going to play around with it some more now, thanks again.

Glad I could help, good luck!