Jordi
Jordi

Reputation: 63

Unity2d: enemies won't spawn at random spawnpoint

In unity I want when the game starts that the enemies spawn at a random spawnpoint (I made 3 spawn points in the game) but the enemies don't spawn at one of the points.

what did I do wrong?

Here is my code:

using UnityEngine; using System.Collections;

public class WaveSpawner : MonoBehaviour {

public enum Spawnstate {SPAWNING, WAITING, COUNTING };

[System.Serializable]
public class Wave
{
    public string name;
    public Transform enemy;
    public int count;
    public float rate;

}

public Wave[] waves;
private int nextWave = 0;


public float timeBetweenWaves = 5f;
public float waveCountdown;

public Transform[] spawnPoints;

private float searchCountdown = 1f;

private Spawnstate state = Spawnstate.COUNTING;

void Start()
{
    if (spawnPoints.Length == 0)
    {
        Debug.LogError("No spawn points referenced");
    }
    waveCountdown = timeBetweenWaves;
}

void Update()
{
    if(state == Spawnstate.WAITING)
    {
        if(!EnemyIsAlive())
        {
            WaveCompleted();
        }
        else
        {
            return;
        }
    }

    if(waveCountdown <= 0)

    {
        if(state != Spawnstate.SPAWNING)
        {
            StartCoroutine(SpawnWave(waves[nextWave]));
        }

    }
    else
    {
        waveCountdown -= Time.deltaTime;
    }
}

void WaveCompleted()

{
    Debug.Log("Wave Completed!");

    state = Spawnstate.COUNTING;
    waveCountdown = timeBetweenWaves;

    if (nextWave + 1 > waves.Length - 1)

    {
        nextWave = 0;
        Debug.Log("Completed all waves!");
    }

    else
    {
        nextWave++;
    }



}

bool EnemyIsAlive()
{
    searchCountdown -= Time.deltaTime;
    if (searchCountdown <= 0f)
    {
        searchCountdown = 1f;
        if (GameObject.FindGameObjectWithTag("Enemy") == null)
        {
            return false;
        }
    }
    return true;
}


IEnumerator SpawnWave(Wave _wave)
{
    Debug.Log("Spawning Wave: " + _wave.name);
    state = Spawnstate.SPAWNING;

    for(int i = 0; i < _wave.count; i++)
    {
        SpawnEnemy(_wave.enemy);
        yield return new WaitForSeconds(1f/ _wave.rate);
    }

    state = Spawnstate.WAITING;


    yield break;
}

void SpawnEnemy(Transform _enemy)

{
    Debug.Log("Spawning Enemy: " + _enemy.name);


    Transform _sp = spawnPoints[ Random.Range (0, spawnPoints.Length) ];
    Instantiate(_enemy, transform.position, transform.rotation);

}
}

enter image description here

Upvotes: 1

Views: 243

Answers (2)

MKougiouris
MKougiouris

Reputation: 2861

Maybe try to change

Transform _sp = spawnPoints[ Random.Range (0, spawnPoints.Length) ];
Instantiate(_enemy, transform.position, transform.rotation);

to something like

Transform _sp = spawnPoints[ Random.Range (0, spawnPoints.Length-1) ];
Instantiate(_enemy, _sp.position, _sp.rotation);

Upvotes: 0

Rian Mostert
Rian Mostert

Reputation: 714

Random.Range returns inclusive range values, so 0 to 5 could include 0 and 5.

An array index is zero based so and array with 5 elements would have 0 to 4.

So in your SpawnEnemy method when you call

spawnPoints[ Random.Range (0, spawnPoints.Length) ]

If Random.Range provides spawnPoints.Length it will fail.

Changing it to

spawnPoints[ Random.Range (0, spawnPoints.Length-1) ]

Should fix your problem.

Upvotes: 1

Related Questions