首页 > 解决方案 > C#/Unity 中的延迟

问题描述

我想在 Unity 中做一个延迟,因为对象不应该立即被销毁。
注意:yield WaitForSeconds (0.25);对我不起作用。它给出了许多错误,例如

; 预期的

即使我有一个。

这是源代码:

using System.Collections.Generic;
using UnityEngine;

public class DestroyObject : MonoBehaviour
{
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.tag == "Enemy")
         {
            //Here 2 Seconds Delay
            Destroy(gameObject);
         }
     }

}

标签: c#unity3d

解决方案


Destroy() 方法的第二个参数可以在销毁之前将浮点数作为延迟,例如:

Destroy(gameObject, 2F);

推荐阅读