Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:19
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/d73e87db5cfebfc9701b227e9750038c to your computer and use it in GitHub Desktop.
Save JonathanYin/d73e87db5cfebfc9701b227e9750038c to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
public class BombSpawner2 : 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;
private float wait = 0f;
public GameObject player1;
public GameObject player2;
void Start()
{
//start spawn
Spawn();
//StartCoroutine(ExecuteAfterTime(10));
}
/*IEnumerator ExecuteAfterTime(float time)
{
yield return new WaitForSeconds(time);
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));
// Code to execute after the delay
}*/
void Spawn()
{
//get random number
float rand = Random.Range(0, 1000);
//if random number is greater than 700 make a bomb
if (rand > 700)
{
if (player1 && player2)
{
Debug.Log("player1 and player2 exist");
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 was a slight modification from my original BombSpawner script, and it is what I used for the "missile" projectiles that I had in my game. Since I realized that the game was slightly difficult with all its new features and obstacles, I changed up the script for one obstacle, making it so that once one player died, they would no longer spawn.

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