hello.
I have this code for an wave spawner script.
in the array _monster i have 3 enemy’s
and in the spawnpoint i have 4 spawnpoint.
but when i want to spawn the 4th enemy because it should do that it says array index is out of range.
using UnityEngine;
using System.Collections;
public class wavescript : MonoBehaviour{
public GameObject[] _monster;
public Transform[] _spawnPoints;
// know your ennemy :)
private string _monsterName;
// 2 seconds between each spawn
private float delayTime = 2f;
private int _oO = 0;
// we start easy with 2 enemies
private int _nbrMonster = 10;
IEnumerator Start(){
int i = 0; //restart
// loop to create a wave using _nbrMonster
for (i = 0; i < _nbrMonster; i++){
Transform pos = _spawnPoints[_oO];
Instantiate(_monster[i], pos.position, pos.rotation);
// I know his name
_monsterName = _monster[i].name;
// change spawnPoint
_oO ++;
if(_oO >= _spawnPoints.Length)_oO = 0;
// wait a moment
yield return new WaitForSeconds(delayTime);
}
}
void Update(){
// while the last enemy lives always we wait
if(!GameObject.Find(_monsterName+("(Clone)"))){
_nbrMonster = _nbrMonster + 2; // increase +2
if(_nbrMonster > 20)_nbrMonster = 20; // maximum occurence
StartCoroutine(Start()); // new wave
}
}
}
i’m not really good in unity so can anybody help me?