Demigiant Forum

Unity Assets => DOTween & DOTween Pro => Topic started by: shawhu on March 22, 2016, 09:24:23 AM

Title: How to delete a gameobject after completion?
Post by: shawhu 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.
Title: Re: How to delete a gameobject after completion?
Post by: Daniele on March 28, 2016, 01:12:30 AM
Hi,

This is sadly a Unity bug with foreach (http://dotween.demigiant.com/support.php?faq=Tweens%20and%20callback%20targets%20inside%20foreach) (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