Question by
Konomira · May 27, 2020 at 02:48 PM ·
destroycoroutinesdestroygameobject
Can't destroy gameobject inside coroutine?
I have the following script:
private IEnumerator WaitForClick()
{
var preview = Instantiate(thumbnail);
while (!Input.GetMouseButtonDown(0))
{
preview.transform.position = Vector3.one * Time.time;
yield return null;
}
Destroy(preview.gameObject);
StopAllCoroutines();
}
However the gameObject is never destroyed. Why is this?
Comment
I tried your line of code and it works fine. Are you sure there is nothing else in your code that might hinder this execution?