- Home /
while i restart the game,it hangs. why so?
var restart=GUI.Button(Rect(30,145,200,50),"restart");
if(restart)
{
Application.LoadLevel(1);
}
This is my code snippet. I tried to restart my scene then the game hangs.I had no idea why it behave so,also tried "Application.LoadLevelAdditive(1);" but no good.
Do anybody have an idea why my game hangs while it restarts?
In your BuildSettings (Ctrl + Shift + B), does your scene have the id 1 ? Do you have any bug reported in the console ?
yes the loading scene has an id 1. i have 3 scenes, first main menu then game_part and last one is score card. i need to invoke the game part when it restarts. the game_part loads it self when it restarts.
Check your console, and let me know if any warning or error.
DeveshPandy , actually when i restart. the whole editor hangs :-)
$$anonymous$$aybe it's because Application.LoadLevel() is called multiple times. Try to put a Debug.Log() in the condition ins$$anonymous$$d of the Application.LoadLevel to check.
Answer by DewDrop · Nov 29, 2012 at 01:32 PM
hi all, thanks for your help. At last i figure out my problem. the problem related to STATIC variables. i made static variables as state flags. one of my static variable remains in false state that triggers the PAUSE command. then when the scene restarts the game hangs without a warning.
once again thank you all.
first, you check how many static variables you are used. then check each one's usage. if you use static variable flags for boolean flags. check time scaling zero on scripts start or awake. hope you will find an answer. here the topic is too old. cheers.
Answer by MonsterLobster · Nov 27, 2012 at 02:05 PM
That is because once you push the button the variable doesnt reset and keeps loading the scene (if your object is set to not get destroyed on load). Maybe use this:
var restart=GUI.Button(Rect(30,145,200,50),"restart");
if(restart)
{
Application.LoadLevel(1);
restart = !restart;
}
I guess this will fix it. Hope it helped!
thanks $$anonymous$$onsterLobster, i tried your method. but don't work at last i understand that the problem was actually related with static variables. it needed to reset some variables before loading the same scene.
Actually the restart resetting will not happen since the scene is already reloaded.
Answer by DeveshPandey · Nov 27, 2012 at 02:45 PM
This will solve your problem. :)
if(GUI.Button(Rect(30,145,200,50),"restart"))
{
Application.LoadLevel(1);
}
hi DeveshPandey, the problem was related with static variables. thank you.
You are most welcome, DewDrop, but which static variable, here not used any.
DeveshPanday, at first i thought the problem is due to this portion because it happens only when i restart the game. after i tried many method suggested by friends here like you and others i fond that the source of problem is somewhere else. then i figure out the right reason is uncleared static variables.
Your answer
Follow this Question
Related Questions
Resetting a Scene 2 Answers
Level Not Loading After Screen Fade 1 Answer
Application.LoadLevel not Resetting 2 Answers
Next Level to unlock when first level is completed 2 Answers