The question is answered, right answer was accepted
GameObject array
I'm trying to create a spawner but for some reason my array doesn't work.
public class spawner : MonoBehaviour {
public GameObject[] enemies;
public Vector3 spawnValues;
public float spawnWait;
public int startWait;
public float spawnMostWait;
public float spawnLeastWait;
int randEnemy;
public bool stop;
// Use this for initialization
void Start () {
StartCoroutine (waitSpawner ());
}
// Update is called once per frame
void Update () {
spawnWait = Random.Range (spawnLeastWait, spawnMostWait);
}
IEnumerator waitSpawner()
{
yield return new WaitForSeconds (startWait);
while (!stop) {
randEnemy = Random.Range (0, 2);
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x,spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
Instantiate (enemies[randEnemy], spawnPosition + transform.TransformPoint (0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds (spawnWait);
}
}
}
For some reason the two Element 0 and Element 1 is missing (not my work's picture)
Thanks in advance :)
I don't think you need the Update function there. That's always going to be resetting the spawnWait, and might be preventing the time from ever being reached.
Put that line that picks a new spawnWait after instantiation.
I put the line after instantiation but the elements problem is still there. because there's no elements in unity, which I can see, I can't put the enemy prefabs which to spawn
Thank you! Please accept the answer, it helps everything flow smoothly around here!
Answer by Tasarran · May 02, 2017 at 04:35 PM
You have to populate your array by dragging some prefabs into it. You'll notice as you drag, when you get over the name of the array, the cursor will change and allow you to drop. Alternatively, change the Size to 2, and you'll see the actual slots where you can drop a prefab.