Loop through foreach and remove a value

Hello

How can I go through a foreach and “remove” the value that I don’t want to use?

I have the following:

public Cubo[5] CubosSctips;
public int ValueToSkip = 2;

and I would like to go through the entire array and remove ValueToSkip value so that no code is executed in it

    private void Start()
    {
        foreach (Cubo cubo in CubosSctips)
        {
            cubo.enabled = false;

        }
    }

A small tip for developament,

in case you need to change an Enumerator(List, Array etc.) runtime use List as type for example, if you are sure that you dont need to change the Enumerator runtime, use an Array.

By change i mean, remove or add an Item.

In your case, everytime you remove or add an Item, you need to copy your array and expand or decrease it depending on your case.

In my case it doesn’t change, that’s why I want to find the solution I asked above :slight_smile:

Is ValueToSkip supposed to be the array index of CubosSctips?
If so, use a for-loop instead:

for(int i = 0, i < CubosSctips.Length; i++)
{
  if(i != ValueToSkip)
  {
    CubosSctips[i].enabled = false;
  }
}
1 Like

I love you <3