Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 显示对象5秒并加载新场景_Unity3d - Fatal编程技术网

Unity3d 显示对象5秒并加载新场景

Unity3d 显示对象5秒并加载新场景,unity3d,Unity3d,我正在制作一个手机游戏,在这个游戏中,我需要在摧毁目标后显示短信5秒钟。我尝试了几秒钟,但没有成功。我还尝试了调用函数。我用行代码SceneManager.LoadScene(“\uu Main1”)创建了一个单独的函数;但我也有同样的问题 我想显示我的文本5秒钟,然后加载新场景 这是我现在的代码: void OnTriggerEnter2D (Collider2D other) { if (other.gameObject.CompareTag ("bomb"))

我正在制作一个手机游戏,在这个游戏中,我需要在摧毁目标后显示短信5秒钟。我尝试了几秒钟,但没有成功。我还尝试了调用函数。我用行代码SceneManager.LoadScene(“\uu Main1”)创建了一个单独的函数;但我也有同样的问题

我想显示我的文本5秒钟,然后加载新场景

这是我现在的代码:

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

        }

我希望有人能帮上忙。谢谢。

您可以使用枚举器等待几秒钟

void OnTriggerEnter2D (Collider2D other)
{
    if (other.gameObject.CompareTag ("bomb")) 
    {
        other.gameObject.SetActive(false);
        StartCoroutine(Foo());
    }
}

IEnumerator Foo()
{
    scoretext.SetActive (true); //this text need to be displayed for 5 seconds after destroying game object
    yield return new WaitForSeconds(5f);
    SceneManager.LoadScene ("__Main1");
}

您应该使用System.Collections添加用于使用枚举数的对象。

您不能销毁该对象并在其上启动协同路由。您需要另一个对象,该对象引用此文本,并在执行协同路由时保持活动状态

编辑:

更准确地说:

void OnTriggerEnter2D (Collider2D other)
{
    if (other.gameObject.CompareTag ("bomb")) {
        other.gameObject.SetActive (false);

        scoretext.SetActive (true); //this text need to be displayed for 5 seconds after destroying game object
        waitEndDisable.WaitAndDisable(scoretext); // here we go ;)
        Destroy (this.gameObject);
    }
在另一个脚本上,运行在活动的游戏对象上:

public void WaitAndDisable(GameObject obj)
{
    StartCoroutine(WaitAndDisableCoroutine(obj));    
}

IEnumerator WaitAndDisableCoroutine(GameObject obj)
{
    yield return new WaitForSeconds(5f);
    SceneManager.LoadScene ("__Main1");
    obj.SetActive(false);
}

正如您在主脚本中看到的,您需要使用WaitAndDisable方法引用脚本。确保此对象处于活动状态(gameObject.activeSelf==true)。

好的,但在我的情况下如何。我一整天都在尝试。已编辑,很抱歉外部的代码,但对此无能为力。此行为红色waitEndDisable.waitEndDisable(scoretext);//好了;)在我的密码上。我在文本元素上附加了新脚本。(因为我需要显示文本并将其保留5秒钟)waitEndDisable是使用我向您展示的方法对脚本的引用;)对不起,它坏了。我不知道。我想这是我的文本对象的问题。我试图在关卡结束时显示分数并保持5秒,但什么都并没有……若你们说什么不起作用,我想人们能帮上更多的忙