Question by
importguru88 · Aug 11, 2016 at 11:45 AM ·
errorspawnintindexspawnpoints
(17,37): error CS0841: A local variable `spawnPointIndex' cannot be used before it is declared
I trying to spawn my enemy ai's but I ran into a problem . I gotten an error saying I did not declare spawpointindex .
Here is my script :
using UnityEngine;
using System.Collections;
public class EnemiesSpawner : MonoBehaviour {
public GameObject enemy;
public Transform [] spawnPoints;
public float spawnTime = 5f;
void Start () {
InvokeRepeating("Spawn", spawnTime, spawnTime); //Calls the "Spawn" function every 10 seconds.
}
void Spawn () {
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
}
}
Comment
Best Answer
Answer by LK84 · Aug 11, 2016 at 12:03 PM
just put int spawnPointIndex = Random.Range (0, spawnPoints.Length);
before Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);