【发布时间】: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