【问题标题】:Display text for 5 seconds after destroying object摧毁物体后显示文字 5 秒
【发布时间】:2015-12-25 14:39:29
【问题描述】:

我正在制作手机游戏,我需要在摧毁物体后显示 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");

        }

我希望有人可以帮助解决这个问题。谢谢。

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    Distroy() 方法有另一个版本,带有 2 个参数:

    Destroy(scoretext, 5f); //destroys scoretext in 5 seconds
    

    【讨论】:

    • 不,我也有同样的问题。是否因为此行代码在 OnTriggerEnter 函数中而影响?
    • 在哪个函数中调用它并不重要。唯一的问题可能是 scoretext 是这个游戏对象(或这个游戏对象本身)的子对象。在这种情况下,您调用 Destroy (this.gameObject);会摧毁它。只要确保您了解Destroy function destroys the game object AND all of its children
    【解决方案2】:

    你只能在协程中产生 return。

    试试

    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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      相关资源
      最近更新 更多