Enemy just keep looping on path and not destroying after path ends

Enemy just keep repeating on path and not destroying after going through last path.

waveconfig

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour
{
[SerializeField] List waveConfigs;
int startingWave = 0;

// Use this for initialization
void Start()
{
    var currentWave = waveConfigs[startingWave];
    StartCoroutine(SpawnAllEnemiesInWave(currentWave));
}

private IEnumerator SpawnAllEnemiesInWave(waveconfig waveconfig)
{
    for (int enemyCount = 0; enemyCount < waveconfig.GetNumberOfEnemies(); enemyCount++)
    {
        var newEnemy = Instantiate(
            waveconfig.GetEnemyPrefab(),
            waveconfig.GetWayPoints()[0].transform.position,
            Quaternion.identity);
        newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveconfig);
        yield return new WaitForSeconds(waveconfig.GetTimeBetweenSpawn());
    }
}

}

Enemypathing

using System.Collections.Generic;
using UnityEngine;

public class EnemyPathing : MonoBehaviour
{
private waveconfig waveconfigobj;
private List waypoints;
private int waypointIndex = 0;

void Start()
{
    if (waveconfigobj != null)
    {
        waypoints = waveconfigobj.GetWayPoints();
        if (waypoints.Count > 0)
            transform.position = waypoints[waypointIndex].position;
        else
            Debug.LogError("No waypoints found in waveconfigobj!");
    }
    else
    {
        Debug.LogError("waveconfigobj is not assigned!");
    }
}

void Update()
{
    if (waypointIndex < waypoints.Count)
    {
        var targetPosition = waypoints[waypointIndex].position;
        var movementThisFrame = waveconfigobj.GetMoveSpeed() * Time.deltaTime;
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementThisFrame);

        if (transform.position == targetPosition)
        {
            waypointIndex++;
            if (waypointIndex >= waypoints.Count)
            {
                waypointIndex = 0;
            }
        }
    }
    else
    {
        Destroy(gameObject);
    }
}

public void SetWaveConfig(waveconfig waveconfig)
{
    if (waveconfig != null)
    {
        this.waveconfigobj = waveconfig;
    }
    else
    {
        Debug.LogError("Trying to set a null waveconfig!");
    }
}

}

Hi lucky_Kumar,

Welcome back! :slight_smile:

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture? And have you already tried to add Debug.Logs to your code to see if the code that is supposed to destroy the enemies gets executed?

If it does not get executed, chedk the if-conditions and the values of the variables at runtime. Maybe the program gets stuck somewhere. That’s often impossible to tell just by reading the code, so make sure to gather data at runtime.


See also:

Hi Nina,

Thanks for the suggestion. I was able to solve it. There was a problem in the if condition in enemy pathing.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms