-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomSpawner.cs
67 lines (57 loc) · 1.48 KB
/
RandomSpawner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using UnityEngine;
using System.Collections;
/**
* Spawns one of the supplied Prefabs over a after a random time-period specified by the time-range parameters.
*/
public class RandomSpawner : MonoBehaviour
{
public GameObject[] spawns;
public float minTime = 0;
public float maxTime = 15;
private bool validPrefabs;
private int prefabsEndIndex = 0;
// Use this for initialization
void Start ()
{
if( spawns.Length == 0 )
{
validPrefabs = false;
Debug.LogWarning( "Spawner '"+ gameObject.name +"' doesn't have any prefabs assigned to spawn!" );
}
else
{
validPrefabs = true;
prefabsEndIndex = spawns.Length;
}
StartCoroutine( "spawnAfterRandomTime" );
}
// Update is called once per frame
// void Update ()
// {
//// Debug.Log( "RandomSpawner Update !! " );
//// maybeSpawn();
//// waitForFive();
//// StartCoroutine( "maybeSpawn" );
// }
// IEnumerator waitForFive()
// {
// Debug.Log ( "time :: " + Time.time.ToString() );
//
// yield return new WaitForSeconds( 5 );
//
// Debug.Log ( "time :: " + Time.time.ToString() );
// }
IEnumerator spawnAfterRandomTime()
{
while ( validPrefabs )
{
float waitTime = Random.Range( minTime, maxTime );
// Debug.Log( "START :: " + Time.time.ToString() );
yield return new WaitForSeconds( waitTime );
int i = Random.Range( 0, prefabsEndIndex );
GameObject.Instantiate(spawns[ i ]);
// Debug.Log( "SPAWN :: " + Time.time.ToString() );
// Debug.Log( "SPAWN :: " + i.ToString() );
}
}
}