【问题标题】:Unity3D - Bug after reloading the sceneUnity3D - 重新加载场景后的错误
【发布时间】:2015-12-24 06:04:54
【问题描述】:

我当前正在创建的手机游戏在游戏结束后显示分数,并重新加载关卡。在文本后面,我正在显示一个背景图像预制件。在我生成图像之前,游戏运行良好。

代码:

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

当我重新加载关卡时,它显示“游戏结束!” - 因为它在我生成图像之前显示 - 并卡在那里。这实际上是生成的预制件的问题,还是其他问题?

感谢您的任何意见。

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

【问题讨论】:

  • Application.LoadLevel("Application.LoadedLevel");需要场景名称作为参数。
  • 对不起,我的错,那里没有引号,只有 Application.LoadedLevel,它加载当前级别。错字。

标签: c# image object unity3d instantiation


【解决方案1】:

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

【讨论】:

  • 但 GameoverText.text 在重新加载后实际上重置为 ""。而我的问题是重装玩完,游戏结束后,不显示BG图和分数。??
猜你喜欢
  • 2013-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多