Question by
kingandroid · Mar 15, 2016 at 12:23 AM ·
destroydestroy object
Coroutine Destroy Callback not working for List of Items
Hey take this for example
List<Someclass> displayItemList = ...;
foreach (var item in displayItemList)
{
item.SetViewScale(Vector3.zero, OnEnd: () => Destroy(item.gameObject));
}
So I loop through bunch of item, and then this is the coroutine
void SetViewScale(Vector3 target, System.Action OnEnd = null)
{
StartCoroutine(Scaling(target, OnEnd));
}
IEnumerator Scaling(Vector3 target, System.Action OnEnd = null)
{
while()
{
// do something
// and finish it
}
if (OnEnd != null)
OnEnd();
}
What I expect is every item is destroyed, but only the first one is destroyed... If I change it to DestroyImmediate
it gives me error that the item has been destroyed, seems like the rest of the loop referencing the first one. Anyone know what's the problem with this?
Comment
I tried using for (int i; ; )
loop and it is still not working, in the end I fixed this by not doing Destroy in the loop. I am still curious though for some reason it points to the first item all the time, so after the first item is destroyed the other code will call that destroyed item...
Your answer
