Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanYin/2724442b87f86ce58af2a693b5e26b71 to your computer and use it in GitHub Desktop.
Save JonathanYin/2724442b87f86ce58af2a693b5e26b71 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class BombSpawner : MonoBehaviour
{
// a public object array for which objects to spawn
public GameObject[] obj;
//min and max times for another spawn
public float spawnMin = 3f;
public float spawnMax = 2f;
void Start()
{
//start spawn
Spawn();
}
void Spawn()
{
//get random number
float rand = Random.Range(0, 1000);
//if random number is greater than 700 make a bomb
if (rand > 700)
{
Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
}
//invoke spawn at random time interval between min and max
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
}
@JonathanYin
Copy link
Author

This is the main script that I used for spawning my in-game objects, such as bonus points and bombs, among others. Using Random.Range, I made the object with this script attached to it spawn at random intervals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment