Click here to Skip to main content
15,883,710 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 arrays, Deffenders and Attackers.
I iterate through the arrays and set the NavMeshAgent destination of the Deffenders to each Attackers position. That all works great.
The problem is that if i have more Deffenders than attackers then i get an error of index out of bounds, and the extra Deffenders dont move.
How can i solve that?
Bellow is my code.
Thank you.

What I have tried:

<pre>
    void Start() 
    {

        for (int i = 0; i < defenders.Length; i++)
        {
            agents.Add(defenders[i].GetComponent<NavMeshAgent>());
        }


    } 
    
    void FixedUpdate() 
    {
       

        for (int j = 0; j < attackers.Length; j++)
        {
            attackersPos = attackers[j].transform.position;
            defendersPos = defenders[j].transform.position;
            
            distanceBetweenEnemies = Vector3.Distance(attackersPos, defendersPos);
            

            if (distanceBetweenEnemies >= 3f)
            {
                
                foreach (var agent in agents)
                {
                    if(defenders.Length > attackers.Length)
                    {
                        foreach(GameObject defender in defenders)
                        {
                            agent.SetDestination(attackers[j].transform.position - spaceBetweenEnemies);
                        }
                    }
                    agent.SetDestination(attackers[j].transform.position - spaceBetweenEnemies);
                    j++;
                }
                

            }

        }
    }
Posted
Updated 31-May-22 21:46pm

1 solution

C#
for (int j = 0; j < attackers.Length; j++)
{
    attackersPos = attackers[j].transform.position;
    defendersPos = defenders[j].transform.position;

You are assuming here that the defenders' array is the same length as that of the attackers. If the defenders' is smaller then you will quite logically get the error you see. You must check the index is valid before you try to use it. Alternatively make sure both arrays contain the same number of items.

[edit]
You also have a problem in this code:
C#
agent.SetDestination(attackers[j].transform.position - spaceBetweenEnemies);
j++;

You are incrementing j, but it is already being incremented by the for statement above, so it could well be set to an invalid number.
[/edit]
 
Share this answer
 
v2
Comments
simple world 1-Jun-22 3:50am    
I do not understand what you are saying, wether its the attackers array or the defenders array in the for loop i get the same result, index out of bound, and the arrays can not be the same length, one will have more that the other.
Thank you.
Richard MacCutchan 1-Jun-22 3:51am    
See my update.
simple world 1-Jun-22 3:54am    
Yes i saw your update thank you.
But if i dont increment the j variable then the gameobjects position in the deffenders array only goes to one gamobject of the attackers array. Imagine i have 10 "enemies" in the Deffendes array and 10 "enemies" in the attackers array, if i dont increment the j then all 10 enemies from the deffenders array will go to one enemie from the attackers array.
Hope it makes sence
Richard MacCutchan 1-Jun-22 4:13am    
Then you need to rethink what your code it trying to do. As it stands you can easily increment j beyond its limit, hence the error.
simple world 1-Jun-22 4:16am    
I want at the begining to have the same number of enemies, the the enemies fight each other and when a enemy from lets say the attackers array dies, then the extra enemies from the deffenders array go to another enemy to fight, and so on.
Do you have any advice?
Thank you for your time

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900