- Home /
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 :) please help me out here !
i use this script now. thats just simple, spawn the number of my enemy prefab i put in.... but this is not the way i want , because when i hit the Start it spawn 100 num of enemies. when the 100 enemies are destroyed there is no more enemies to shoot at....
so...
i want my new spawner to every 3 - 4 seconds spawn Instantly random spawn between 5 different enemy Prefabs i got.
so my enemies can keep going on and on and on and on.. forever.. and ever
((($$anonymous$$y Old Enemy Spawner Script))
public GameObject enemyPrefab;
public float numEnemies;
public float x$$anonymous$$in = 19F;
public float x$$anonymous$$ax = 85F;
public float y$$anonymous$$in = 3.5F;
public float y$$anonymous$$ax = -4.5F;
void Start () {
GameObject newParent = GameObject.Find("1 - Background Elements");
for (int i = 0; i < numEnemies; i++)
{
Vector3 newPos = new Vector3(Random.Range(x$$anonymous$$in, x$$anonymous$$ax), Random.Range(y$$anonymous$$in, y$$anonymous$$ax), 0);
GameObject octo = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
octo.transform.parent = newParent.transform;
}
}
}
Hey! I was follow the guide above but I got an error :(
Assets/PlatformSpawner.cs(8,37): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `PlatformSpawner.platformPrefabs'
Any idea? Thank you!
using UnityEngine;
using System.Collections;
public class PlatformSpawner : $$anonymous$$onoBehaviour {
public GameObject [] platformPrefabs;
GameObject platformPrefab = platformPrefabs[Random.Range(0, platformPrefabs.Length)];
private void SpawnPlatform()
{
Vector3 newPos = new Vector3 (3.5, 1.8, 0);
GameObject octo = Instantiate (platformPrefab, newPos, Quaternion.identity) as GameObject;
}
void Start()
{
for (int i = 0; i < numPlatforms; i++)
{
SpawnPlatform ();
}
}
private float timeUntilSpawn = 0;
public void Update()
{
timeUntilSpawn -= Time.deltaTime;
if (timeUntilSpawn <= 0)
{
SpawnPlatform();
}
}
}
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;
}
Answer by Chris_Dlala · Jan 06, 2015 at 04:00 PM
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:
Choosing a random prefab to instantiate
Create a spawn method
Call the spawn method repeatedly
Choosing a random prefab to instantiate
o 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
ecause 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
or 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
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
Answer by KevinRF · Jan 08, 2015 at 12:35 PM
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 :)
Answer by SilentFootsteps · Aug 27, 2017 at 05:52 PM
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.
Your answer
Follow this Question
Related Questions
Random Spawn, Random Prefab 2 Answers
How can I make my prefab fire out at a random velocity every 2 secs 2 Answers
instantiate random prefabs based on player camera distance 1 Answer
Semi Random Monster Spawner 3 Answers
How to randomly generate prefab? 2 Answers