Hello there!
Trying complete the challenge I did a different code of Rick and it worked just fine, but I started to wonder if what I did is not, let’s say, “good practice” on coding.
Is there any reason why I should avoid doing with a Int instead of a Attacker?
Here is my code:
public class AttackerSpawner : MonoBehaviour {
[SerializeField] bool spawn = true;
[SerializeField] int minRandomTime = 3;
[SerializeField] int maxRandomTime = 7;
[SerializeField] Attacker[] enemyArray;
IEnumerator Start ()
{
do
{
yield return StartCoroutine(SpawnAttacker());
}
while (spawn);
}
private IEnumerator SpawnAttacker()
{
yield return new WaitForSeconds(Random.Range(minRandomTime, maxRandomTime));
int enemyIndex = Random.Range(0, enemyArray.Length);
Spawn(enemyIndex);
}
private void Spawn(int index)
{
Attacker newAttacker = Instantiate(enemyArray[index], new Vector2(transform.position.x, transform.position.y), Quaternion.identity) as Attacker;
newAttacker.transform.parent = transform;
}
}
Thanks for your time!