Need help with an array. - C#

I have a very simple problem, but its hard to explain, so bare with me if it might be confusing:

I need a function that can find the specific array-number of a gameObject in an array. I need this so I can remove that gameObject from the array. And only that gameObject.

-thanks :slight_smile:

to get element:

GameObject go3 = MyGameObjects[3];

to remove element just use List instead of GameObject and simply use Remove method on List<>

  • if you just need to remove and needn’t an index, simply use
foreach(GameObject go in gos)
{
    gos.Remove(go);
}
  • simply this script looks like
gos.Clear();
  • if you need an index use
gos.IndexOf(go);

important

never change array while you use it in foreach cycle. especially you completely understand what you are doing

Use generic lists - much faster and properly support inserting and deleting elements as well as supporting standard array syntax:

  using System.Collections.Generic;

  ...

  List<GameObject> gos = new List<GameObject>();
  gos.Remove(go);
  var i = gos.IndexOf(go);