x


Can't go back to the game scene from main menu for the second time

So, I have a button in main menu with this code

var soundhover : AudioClip;
var beep : AudioClip;
var QuitButton : boolean = false;
function OnMouseEnter(){
audio.PlayOneShot(soundhover);
}  function OnMouseUp(){
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.35);
if(QuitButton){
    Application.Quit();
}
else{
    Application.LoadLevel("tanktes");
}
} @script RequireComponent(AudioSource);

I can do the LoadLevel alright for the first time. And then I pause Ingame and go back to main menu by clicking a code. Here's the pause code :

    if(Input.GetButtonUp("Esc"))
{
doWindow0 = true;
Screen.lockCursor = false;
Screen.showCursor = true;
Time.timeScale = 0;
}

And here's the Back to menu button code:

    if(GUI.Button (Rect (10,30, 150,20), "Back to Menu"))
    Application.LoadLevel("StartMenu");

And I can go back to the main menu. But this time, the play game button AND the quit button are not working (tested it on the .exe file). The audio when the mouse is hovering plays fine so we can assume the script is working, but it's not going back to the game scene. What seems to be the cause of this? Helps appreciated.

more ▼

asked Oct 30 '11 at 01:25 AM

meazant gravatar image

meazant
31 3 3 5

Maybe you should put Time.timeScale back to 1.0

Oct 30 '11 at 06:44 AM Branislav

Does anyone know whether Update still happens if timeScale is set to 0? Someone should test that.

Oct 30 '11 at 06:51 AM syclamoth

Just tested it- Yes, yes it does.

Oct 30 '11 at 06:55 AM syclamoth

If I write :

if(GUI.Button (Rect (10,30, 150,20), "Back to Menu"))

    Time.timeScale = 1.0;

    Application.LoadLevel(level);

When I pause the game it suddenly go back to the main menu without even clicking the "Back to Menu" button..

Oct 30 '11 at 08:06 PM meazant
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Thank you for all of your help, I've found the problem. Yes, I need to turn back the Time.timeScale to 1 and also I have an error in the if statement. It lacks a {}. Newbish mistake and I am sorry for that. Nevertheless, always glad to get such speedy responses from Unity3D Answers people. Have a nice day :)

more ▼

answered Oct 31 '11 at 06:06 AM

meazant gravatar image

meazant
31 3 3 5

This is why I always put the brackets around if statements, even if they are only one line long. It gets you in the habit for when it's important.

Oct 31 '11 at 06:09 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

Try adding an extra variable called something like 'levelToLoad' and make it a string variable. Then, replace any Application.loadLevel("levelname"); with levelToLoad. Everytime you click on the object which will load the level, it with an object in your scene, goto where it says Level To Load and put in the name of your level. That works for me.

*var levelToLoad : String;*
var soundhover : AudioClip;
var beep : AudioClip;
var QuitButton : boolean = false;
function OnMouseEnter(){
audio.PlayOneShot(soundhover);
}
function OnMouseUp(){
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.35);
if(QuitButton){
Application.Quit();
}
else{
Application.LoadLevel(levelToLoad);
}
}
@script RequireComponent(AudioSource)

Hopefully that will work.

more ▼

answered Oct 30 '11 at 12:42 PM

Computoguy gravatar image

Computoguy
41 4 7 9

Oct 30 '11 at 07:42 PM meazant
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x263
x261
x57
x52
x48

asked: Oct 30 '11 at 01:25 AM

Seen: 863 times

Last Updated: Oct 31 '11 at 06:09 AM