Created
July 17, 2017 16:18
-
-
Save JonathanYin/2724442b87f86ce58af2a693b5e26b71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.