- Home /
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.
P.S. This ONLY happens when i reload the level. In the first run, all the intended text and BG image is displayed as expected.
For starters, I assume Application.LoadLevel("Application.LoadedLevel"); should actually be Application.LoadLevel(Application.loadedLevel);
I'm sorry, that's a typo. In the code, there's no quotes. Thanks
Answer by PabloUnityArgentina · Dec 27, 2015 at 11:03 AM
The GameoverText.text is a static property. It belongs to the class, not to the object. So no matter how many times you reload the scene. It remains the same, because the statics members doesn't belong to any scene. In order to resolve this, you have to restart your static property when you reload the scene.
But my problem is it's not displaying the BG image in the second run. Is is something do with Gameover Text??
You are setting the parent of a prefab in the line 15, here:
GameOverBGAsset1.transform.SetParent(GameOverCanvas.transform);
"GameOverBGAsset1" is a prefab, not a GameObject .It is not instantiated in a scene, so that line of code is wrong. You have to change it in orden to set the parent of the real GameObject, instantiated in the scene, not the prefab:
GameOverImg2.transform.SetParent(GameOverCanvas.transform);
Thanks $$anonymous$$en You Save $$anonymous$$y day. i don't know that static property needs to restart. Thank you
U saved me =)
p.s. For those who don't know where to reset your static variables - you can reset them right in your LoadScene method. Like this:
public void RestartGame()
{
SceneManager.LoadScene("SampleScene");
GameControllScript.dinamiteCratePicked = false;
GameControllScript.netCratePicked = false;
GameControllScript.netCount = 0;
GameControllScript.dinamiteCount = 0;
}
where GameControllScript is the name of script with your static variables.
Answer by Wilson28300 · Dec 27, 2015 at 09:22 AM
I think it should be Application.LoadLevel(Application.LoadedLevel); without double quotes. Otherwise it will check for a scene named "Application.LoadedLevel".
I'm sorry, that's a typo. In the code, there's no quotes. Thanks
Your answer
Follow this Question
Related Questions
Instantiate a prefab just one time 1 Answer
Instantiate Prefab (Image) and show it on the screen? 1 Answer
Prefabs dont instantiate directly on point 0 Answers
Referencing gameObject from script after Instantiate 0 Answers
Prefab of a whole scene? 3 Answers