Random Prefab Spawner C#

Hello Guys I’m stock whit my first IOS 2D Game project, i need a Random Prefab Spawner Script in C#,

whit some public values, so i can put in my prefabs and then i just want the script to Instantly between 5 seconds randomly spawn between the all prefabs i put in :slight_smile:
please help me out here !

Please post your script.

–

you can't choose a member of platformPrefabs until the code knows it has been initiated, try: public GameObject[] platformPrefabs; GameObject platformPrefab; private void SpawnPlatform(){ if(platformPrefabs == null || platformPrefabs.Length < 1){ return; } platformPrefab = platformPrefabs[Random.Range(0, platformPrefabs.Length)]; Vector3 newPos = new Vector3 (3.5f, 1.8f, 0f); GameObject octo = Instantiate (platformPrefab, newPos, Quaternion.identity) as GameObject; }

–

3 Answers

3

Hi! Firstly, I only want to give you enough of an understanding so you can give writing what you want a go. I’m going to break down your problem into a number of steps:

  1. Choosing a random prefab to instantiate
  2. Create a spawn method
  3. Call the spawn method repeatedly

Choosing a random prefab to instantiate

To do this we need a public array of GameObjects (prefabs) to choose from. This is done by adding β€œβ€ to the end of the variable type (it’s usually best to make the array name a plural):

public GameObject[] enemyPrefabs;

Now to access an element or single prefab you pass in an index between 0 and the number of entries in the array or β€œLength” property. So to get a random entry you would use:

GameObject enemyPrefab = enemyPrefabs[Random.Range(0, enemyPrefabs.Length)];

Then you can use that variable to instantiate an enemy in the scene.

Create a spawn method

Because you are now spawning enemies at the start of the game, and then periodically throughout the game, you should wrap this functionality in one method that you can call repeatedly whenever you require an enemy to be spawned. For example:

private void SpawnEnemy()
{
    Vector3 newPos = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
    GameObject octo = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
}

// Example of calling method in Start...

for (int i = 0; i < numEnemies; i++)
{
    SpawnEnemy();
}

Call the spawn method repeatedly

For repeated spawn there are many ways to do this. What you suggest is simple, you want a countdown to each spawn. For this you simply need to add an Update method and a time variable to track how long until the next spawn. Subtract the delta time from your countdown, and when the countdown reaches <= 0 spawn your enemies and reset the timer:

public float spawnCooldown = 1;
private float timeUntilSpawn = 0;

public void Update()
{
    timeUntilSpawn -= Time.deltaTime;
    if(timeUntilSpawn <= 0)
    {
        // Do your enemy spawns here
        ...
        // Reset for next spawn
        timeUntilSpawn = spawnCooldown;
    }
}

I hope that helps explain how to approach what you wanted. =D

Did that explain enough of what you needed? If so, could you accept this answer =D

–

Hey Thanks man,
I toke me some time to understand it, but now it works and thats total awesome! , thank you very much ! you are the best !!! =D :slight_smile:

What if you wanted to spawn the game objects, assigning their position to empty cells in a panel (UI layout)? Like adding random items to an inventory system for example.