我正在制作手机游戏,我需要在销毁对象后显示短信5秒钟。我尝试使用yield waitforseconds但是不起作用。
这是我现在的代码:
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("bomb")) {
other.gameObject.SetActive (false);
Destroy (this.gameObject);
scoretext.SetActive (true); //this text need to be displayed for 5 seconds after destroying game object
SceneManager.LoadScene ("__Main1");
}
我希望有人可以帮忙解决这个问题。感谢。
答案 0 :(得分:0)
Distroy()方法有另一个带有2个参数的版本:
Destroy(scoretext, 5f); //destroys scoretext in 5 seconds
答案 1 :(得分:0)
你只能在协程中产生回报。
尝试
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag ("bomb"))
{
other.gameObject.SetActive (false);
StartCoroutine(DeferredDestroy());
}
}
IEnumerator DeferredDestroy()
{
this.gameObject.SetActive(false);
yield return new WaitForSeconds(5.0f);
Destroy (this.gameObject);
scoretext.SetActive (true);
SceneManager.LoadScene ("__Main1");
}