• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NickMalmlief · Oct 01, 2020 at 09:02 AM · for-loop

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[i].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<Transform> 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<PathStartStop>();
     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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NickMalmlief · Oct 03, 2020 at 05:54 AM

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

More than one row in an array not displaying in a FOR loop 2 Answers

using "for" in Unity Script HELP! 2 Answers

Difference between if statement, while and for loop? 4 Answers

Why is my FOR loop Accelerating each time it is used? 2 Answers

For loop multiplying the action by the lenght of the array.. WHY ?! 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges