For Loop not working for some elements of List with specific Int variable

I am working on a 2D Shmup. Many enemies spawn in waves that follow paths from point to point. At each point there can be a bool activated stopMove that halts the movement of the entire wave. Movement is then started again for the whole wave after a specified time. When enemies spawn they are placed in a List within a scriptable object called WaveConfig. I use that List to start and stop movement through a method called StopAndStartMovement(), but also use it to increment the waypointIndex (next point the enemies in a wave will move towards).

What happens is that one or more enemies in waves don’t have their waypointIndex incremented (used Debug.Log to figure this out). All other variables are universally affected as expected in all cases I have seen.

What am I missing? I know it’s not Unity/C#; it’s me. I am new to scripting… I’m sure there is some optimization I could do as well, btw.

Here is the WaveConfig Scriptable Object Script used to direct movement parameters for enemies in a wave:

{

[SerializeField] GameObject enemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float timeBetweenSpawns = 2.0f;
[SerializeField] float timeBetweenWaves = 3.0f;
[SerializeField] float spawnRandomFactor = 0.3f;
public float moveSpeed = 2.0f;
[SerializeField] int numberOfEnemies = 5;
[SerializeField] int waveNumber;
public List<GameObject> spawnedInWave = new List<GameObject>();
public bool rotateEnemy;

public GameObject GetEnemyPrefab() { return enemyPrefab; }

public List<Transform> GetWaypoints()
{
    var waveWayPoints = new List<Transform>();
    foreach (Transform child in pathPrefab.transform)
    {
        waveWayPoints.Add(child);
    }
    return waveWayPoints;
}

public void StopAndStartMovement()
{
    for (int i = spawnedInWave.Count - 1; i >= 0; i--)
    {
        EnemyPathing enemyPathing = spawnedInWave*.GetComponent<EnemyPathing>();*

enemyPathing.waypointIndex++;
enemyPathing.hasStopped = true;
enemyPathing.executeMove = false;
enemyPathing.StartCoroutine(enemyPathing.RestartMovement());
}
}
public int GetWaveNumber() { return waveNumber; }
public float GetTimeBetweenSpawns() { return timeBetweenSpawns; }
public float GetTimeBetweenWaves() { return timeBetweenWaves; }
public float GetSpawnRandomFactor() { return spawnRandomFactor; }
public float GetMoveSpeed() { return moveSpeed; }
public int GetNumberOfEnemies() { return numberOfEnemies; }
public GameObject GetPathPrefab() { return pathPrefab; }
}
Here is the EnemyPathing Script attached to each enemy in a wave
WaveConfig waveConfig;
List waypoints;
public int waypointIndex = 0;
private Vector3 targetDirection;
public float rotationSpeed = 2.5f;
public int enemyWave;
GameObject pathPrefab;
bool stopMove = false;
float stopMoveDuration = 1.5f;
public bool executeMove = true;
bool rotate;
public bool hasStopped = false;
private void Start()
{
waypoints = waveConfig.GetWaypoints();
transform.position = waypoints[waypointIndex].transform.position;
}
private void Update()
{
Move();
}
public void SetWaveConfig(WaveConfig waveConfig)
{
this.waveConfig = waveConfig;
pathPrefab = waveConfig.GetPathPrefab();
enemyWave = waveConfig.GetWaveNumber();
rotate = waveConfig.rotateEnemy;
}
private void Move()
{
if (executeMove == true)
{
if (waypointIndex <= waypoints.Count - 1)
{
if (rotate == true)
{
WithRotation();
}
else { WithoutRotation(); }
}
else
{
Destroy(gameObject);
}
}
}
void WithRotation()
{
var targetPosition = waypoints[waypointIndex].transform.position;
Debug.Log("targetPosition is: " + targetPosition);
var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;
transform.position = Vector2.MoveTowards
(transform.position, targetPosition, movementThisFrame);
Vector3 vectorToTarget = targetPosition - transform.position;
float angle = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) + 90;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotationSpeed);
if (transform.position == targetPosition)
{
StartCoroutine(ProgressMovement());
}
}
void WithoutRotation()
{
var targetPosition = waypoints[waypointIndex].transform.position;
var movementThisFrame = waveConfig.GetMoveSpeed() * Time.deltaTime;
transform.position = Vector2.MoveTowards
(transform.position, targetPosition, movementThisFrame);
if (transform.position == targetPosition)
{
StartCoroutine(ProgressMovement());
}
}
IEnumerator ProgressMovement()
{
PathStartStop pathStartStop = pathPrefab.GetComponent();
stopMove = pathStartStop.StopMovement(waypointIndex);
if (stopMove == true && hasStopped == false)
{
Debug.Log("waypointIndex is: " + waypointIndex);
executeMove = false;
waveConfig.StopAndStartMovement();
Debug.Log("waypointIndex is: " + waypointIndex);
}
else if (stopMove == false)
{
waypointIndex++;
}
yield return null;
}
public IEnumerator RestartMovement()
{
yield return StartCoroutine(DelayMovement());
executeMove = true;
}
IEnumerator DelayMovement()
{
yield return new WaitForSeconds(stopMoveDuration);
}
}

I solved this one myself after all. Looking at the code after taking some space from it, I realized the problem was the waypointIndex actually WAS incrementing for each enemy in a wave. It simply didn’t give each enemy the same result as they are at different waypoints when movement is stopped. Pretty simply, actually. Just posting this answer in case it ends up being helpful for someone…