How to delete a gameobject after completion?
« on: March 22, 2016, 09:24:23 AM »
List<GameObject> mylist=GetMyList();
foreach(var item in mylist){
  item.transform.DOMove (position, 2, false).OnComplete(()=>{Destroy(item.gameobject);})
}

This doesn't work. When items complete one by one, they will always delete the last item in the list.

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: How to delete a gameobject after completion?
« Reply #1 on: March 28, 2016, 01:12:30 AM »
Hi,

This is sadly a Unity bug with foreach (should be fixed when they update to a newer NET version).
Just reassign your item inside the loop and it will work:

Code: [Select]
foreach(var item in mylist){
  var it = item;
  it.transform.DOMove (position, 2, false).OnComplete(()=>{Destroy(it.gameobject);})
}

Cheers,
Daniele