Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Application.LoadLevel Isn't Working

Discussion in 'Scripting' started by Evil Square, Jun 27, 2015.

  1. Evil Square

    Evil Square

    Joined:
    Mar 21, 2015
    Posts:
    18
    In my game, when the game is paused, if you click the menu button, it will return you to the main menu, titled "Title." However, it simply reloads the current level which is named "Gameplay." Here's the code for the menu button:

    Code (JavaScript):
    1. function OnPressed ()
    2. {
    3.         Time.timeScale = 1.0;
    4.    
    5.         Application.LoadLevel ("Title");
    6. }
    I first have to unpause the game, thus I set the time scale to normal. Then I go to the "Title." However, clicking the button just reloads the current scene "Gameplay." Is there anything wrong with this code?

    I have a similar script attached to the play button in the title screen which works just fine. Here's the code for the play button:

    Code (JavaScript):
    1. function OnPressed ()
    2. {
    3.         Application.LoadLevel ("Gameplay");
    4. }
    And yes, both scenes are in the build settings which can be seen here:
    0.png

    I've tried removing the Time.timeScale = 1.0; but it still did not work. I have also tried loading a different scene which also didn't work. Lastly, I have tried loading the level by index, being 0 for the title screen, and THAT didn't work. I've used Application.LoadLevel a lot, but I've never come across this problem. Could it be a problem inside Unity and not my doing?

    Thanks,

    -Chris
     
  2. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    How do you use buttons ? I mean, are you using button UI functions integrated in them and calling "OnPressed" function when you press the button ? Like this.
     

    Attached Files:

  3. Evil Square

    Evil Square

    Joined:
    Mar 21, 2015
    Posts:
    18
    Yes.
     
  4. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    How do you do that if you don't set functions as 'public' ? Anyway, the only thing I'm thinking about now is to rename one of your two functions differently from the other, like 'OnPressed_1' or 'LoadTitle' and leave the other with its name. I'm not very sure it will fix your problem, but try to do this for now.
     
  5. Evil Square

    Evil Square

    Joined:
    Mar 21, 2015
    Posts:
    18
    I tried changing the name to LoadTitle, but it still doesn't work. It still simply resets the current level.

    I'm starting to think that for some reason, Unity is confusing these two scenes. It loads the level "Gameplay" when it should be loading "Title."

    EDIT: Now that I think about it, it might actually be a problem within the script attached to the main character. If I disable the main character and use the menu button, the menu button returns me to the title screen as it should. I'm thinking some function within my main character is overriding the load level. I will need to look through the many functions on my main character to identify the problematic source though.
     
  6. Evil Square

    Evil Square

    Joined:
    Mar 21, 2015
    Posts:
    18
    Alright, I fixed it. It was a simple redundant function in the player script that would reload the level during OnBecameInvisible. I do this in another function, so this function was not needed, so I removed it. This allows the menu button to bring you to the title screen.

    I don't think removing that seemingly extra piece of code will mess things up later. -.-

    Anyway, thanks for the help! :)
     
  7. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Alright, happy for you then. :)