简体   繁体   English

Unity3D-重新加载场景后的错误

[英]Unity3D - Bug after reloading the scene

The mobile game i'm currently creating displays the score after Game over, and reloads the level. 我当前正在创建的移动游戏在游戏结束后显示分数,并重新加载关卡。 Behind the text i'm displaying an background image prefab. 在文本后面,我正在显示背景图像预制件。 The game works fine until i spawn the image. 在我生成图像之前,游戏运行正常。

Code: 码:

    // set gameOver flag to true.
    public void GameOver()
    {
        GameoverText.text = "Game Over!";
        gameOver = true;
    }

    ......
    ......
    // Instantiate GameOverBGAsset1 -- the image prefab, and GameOverCanvas -- a reference to 'Canvas2', And make GameOverBGAsset1 a child of Canvas2.
    if (gameOver)
    {
                GameObject GameOverImg2 = Instantiate(GameOverBGAsset1, GameOverBGPos.transform.position, Quaternion.Euler(0, 0, 0)) as GameObject;
                GameObject GameOverCanvas = GameObject.FindGameObjectWithTag("Canvas2");
                GameOverBGAsset1.transform.SetParent(GameOverCanvas.transform);

                restartText.text = "Tap to Replay";
                scoreTextLarge.text = "Score: " + score;
                restart = true;

                break;
            } 

   ....
   ....
   // restart when screen is touched
   if (restart)
   {
               tap = Camera.main.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 5));            
               if ((tap.x != 0) || (tap.y != 0) || (tap.z != 0))
               {
                 Application.LoadLevel("Application.LoadedLevel");
               }
    }

When i reload the level it displays "Game Over!" 当我重新加载关卡时,它会显示“游戏结束!” -- coz it's displayed before i spawn the image -- and get stuck there. -因为它在我生成图像之前已经显示-并且卡在了那里。 Is it actually a problem with the spawned prefab, or something else? 产生的预制件实际上是问题,还是其他问题?

Thanks for any input. 感谢您的任何投入。

EDIT: This ONLY happens when i reload the level. 编辑:这仅在我重新加载级别时发生。 In the first run, all the intended text and BG image is displayed as expected. 在第一次运行中,所有预期的文本和BG图像均按预期显示。

If you relod scene you restart only non-static variables . 如果您放逐场景,则仅重新启动非静态变量 After load scene variable GameoverText.text still contains value "Game Over!". 加载后,场景变量GameoverText.text仍包含值“ Game Over!”。

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

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