Unity3D - 重新加载场景后的Bug

时间:2015-12-24 06:04:55

标签: c# image object unity3d instantiation

我正在创建的手机游戏在游戏结束后显示分数,并重新加载该级别。在文本后面我显示了预制的背景图像。游戏工作正常,直到我产生图像。

代码:

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

当我重新加载关卡时,它会显示" Game Over!" - 因为它在我产生图像之前显示 - 然后卡在那里。它实际上是产生预制件的问题,还是别的什么?

感谢您的任何意见。

编辑:这只发生在我重新加载关卡时。在第一次运行中,所有预期的文本和BG图像都按预期显示。

1 个答案:

答案 0 :(得分:0)

如果您重新定位场景,则仅重新启动非静态变量。加载后场景变量GameoverText.text仍包含值“Game Over!”。