-2
\$\begingroup\$

I am using Unity 4.6.1 and I am working on a game project. I created a JS script with Application.LoadLevel to go to the next scene. Here is my code for OnGUI:

function OnGUI () {

{

Application.LoadLevel ("Level1");

}

}

When I tested the game the first time, everything worked fine, but the second time, it shows the menu for a few seconds and goes directly to the first level. I did the same thing with another scene and another JS script then changed my build settings to see if that would help. It did the same thing like the first JS script. How can I fix this so that the level loads then the player presses a button instead of it loading immediately?

\$\endgroup\$
  • \$\begingroup\$ So, you don't want "Level1" to be loaded immediately? If so, you'll need to add some code to control when Application.LoadLevel() is called. Right now it's being called in the first frame that this object is enabled (because that's when OnGUI() happens). You'll need to clarify in your question when you want the level to be loaded instead - eg. after a delay, or only when the player presses a particular button, etc. \$\endgroup\$ – DMGregory Apr 6 '15 at 23:00
  • \$\begingroup\$ I edited the question at the end. \$\endgroup\$ – kprovost7314 Apr 6 '15 at 23:09
3
\$\begingroup\$

You may want to create a button for the script.

void OnGUI() {
    if (GUI.Button(new Rect(10, 10, 150, 100), "Go to Level1"))
        Application.LoadLevel ("Level1");

}

For more information about the GUI scripting, check this out: http://docs.unity3d.com/Manual/gui-Basics.html

\$\endgroup\$
  • \$\begingroup\$ The funny thing is that I have a book with the exact same type of code. Thanks! \$\endgroup\$ – kprovost7314 Apr 7 '15 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.