I’m having an issue with one of my scripts. What I’m trying to do is instantiate my enemy prefab at random times and I had success in that.
My only problem is that I want it to spawn at random but I don’t want the spawn points to spawn 3 in a row like you can see above. Here is my spawning script:
using UnityEngine;
using System.Collections;
public class spawnEnemy : MonoBehaviour {
public float time = 1.0F;
float realTime;
public GameObject enemy;
Random rnd = new Random();
public bool instancedPrefab = false;
// Use this for initialization
void Start () {
realTime = time;
StartCoroutine(enemySpawn());
StartCoroutine(timeChanger());
}
// Update is called once per frame
void Update () {
//GameObject.Instantiate(enemy);
}
IEnumerator enemySpawn()
{
while(true)
{
yield return new WaitForSeconds(realTime);
enemy.transform.position = this.gameObject.transform.position;
Instantiate(enemy);
}
}
IEnumerator timeChanger()
{
while(true)
{
yield return new WaitForSeconds(2);
int randFirst = Random.Range(1, 3);
int randSeccond = Random.Range(4, 6);
int randNumber = Random.Range(randFirst, randSeccond);
realTime = randNumber;
}
}
}