- Home /
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 :)
Answer by ScroodgeM · Jul 21, 2012 at 08:25 PM
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 doingthnx dude :D... You really helped me out :)
It turned out that the solution I had thought up in my head didnt work at all... But this helped me to realize that it was a huge detour :)
Answer by ScroodgeM · Jul 21, 2012 at 08:02 PM
to get element:
GameObject go3 = MyGameObjects[3];
to remove element just use List<GameObject> instead of GameObject[] and simply use Remove method on List<>
yeah. But I need a funtion. Like:
foreach(GameObject go in gos)
{
int goInt;
go.GetArrayNumber... stored in goInt
gos.Remove(arrayNumber(goInt));
}
Answer by whydoidoit · Jul 21, 2012 at 08:31 PM
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);
Thanks :) ... But I already know that :) ... thumbs up though :)