Home > front end >  Unity for loop try to access a destroyed GameObject
Unity for loop try to access a destroyed GameObject

Time:06-09

I'm working on a brickout game clone, so I'm trying to add a multiply the ball feature to the game. This Video shows the feature.

Here is my code:

//access to all active balls
Ball[] balls =  FindObjectsOfType<Ball>();
//loop for the length of the balls
for (int i = 0; i <= balls.Length; i  )
{
    if (balls.Length >= 1000) 
    {
        return;
    }
    else 
    {
        // here I'm trying to fix the issue by checking if the ball exists, so I will call the function if not. return. but console show me same message
        if (balls[i] != null)
        {
            balls[i].CallM3x();
            i  ;
         }  
    }
}

The unity console message is:

IndexOutOfRangeException: Index was outside the bounds of the array.

My code above is working fine, but the issue is some balls destroy before the for loop end its work by less than a second .

Any idea how to avoid this?

CodePudding user response:

A simple way to avoid the issue of re-referencing objects you have deleted in a loop is by counting i down from balls.Length to 0 instead of up from 0 to balls.Length.

for (int i = balls.Length - 1; i >= 0; i--)

Also, you might want to look again at your for-loop: you are going up to <= balls.Length, allowing the highest index to be = balls.Length. That index is out of bounds though, because we start counting at 0. I suggest using < instead of <= here.

CodePudding user response:

Ball[] balls = FindObjectsOfType<Ball>();
for (int i = 0; i < balls.Length; i  )
{
    if (balls.Length >= 1000)
    {
        return;
    }
    else
    {
        if (balls[i] != null)
        {
            balls[i].CallM3x();
            // i  ; <- This is your problem. Remove it.
        }
    }
}

CodePudding user response:

There are two fundamental issues with your loop:

  1. you're looping up to i <= balls.Length which will always throw an error (there is no a[10] in an array of length 10).

  2. you're incrementing i again within the loop, which will skip items

Change the loop condition to i < balls.Length and remove the i within the loop.

Also, you don't need to check if (balls.Length >= 1000) within the loop every time - you can check that before the loop (but what's the problem with a collection with more than 10,000 items?)

the issue is some balls destroy before the for loop end its work by less than a second

I don't see how this can be a problem. You're creating an array of references that you're looping over. Even if items are destroyed within the game, the object references still exist within your array. There may be some other flag to tell you that an object is destroyed (I am not a Unity expert), but they would not change to null inside your array.

  • Related