简体   繁体   English

统一问题 c# - Object.Destroy 不起作用

[英]Problem with unity c# - Object.Destroy not working

So I am creating many objects in coroutine and of course I want to delete them, but Object.Destroy doesnt work.所以我在协程中创建了许多对象,当然我想删除它们,但是 Object.Destroy 不起作用。 Any ideas?有任何想法吗?

`public class Spawner : MonoBehaviour
{
    [SerializeField] Obstacle obstaclePrefab;
    private void Start()
    {
        StartCoroutine(SpawnNewObstacle());
    }
    IEnumerator SpawnNewObstacle()
    {
        do
        {
            float range = Random.Range(-2.55f, 3.35f);
            Vector3 position = new Vector3(12.5f, range, -1);
            var obstacle = Instantiate(obstaclePrefab, position, Quaternion.identity);
            yield return new WaitForSeconds(2f);
            DestroyImmediate(obstacle);
        }
        while (true);
    }
}`

I guess, instead of DestroyImmediate you are using Object.Destroy , which is a recommended practice.我想,您使用的是Object.Destroy而不是DestroyImmediate ,这是推荐的做法。

Eventually the objects gets destroyed, the destruction is delayed till the objects do not have any refernces.最终对象被销毁,销毁被延迟,直到对象没有任何引用。

Especially when you are using coroutines, the execution is paused and the control is returned to the calling code and your objects may be still in use.特别是当您使用协程时,执行会暂停,控制权会返回给调用代码,您的对象可能仍在使用中。 In that case, the delay in destruction is more visible.在这种情况下,破坏的延迟更加明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM