Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Unsolved] Cannot Get Application.LoadLevel to work

Discussion in 'Scripting' started by ajsnarr98, Nov 27, 2015.

  1. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    For some reason Application.LoadLevel is not working for me. It does not give an error message, or anything else, it just gets skipped over. I added some Debug.Log statements for testing, and for some reason the error gets logged, and the "this should not show" gets logged, but the MainScreen level never loads. I tried Application.LoadLevel(0) but that does not work either.

    Here is the part of my code where it is:
    Code (csharp):
    1.  
    2. if(!error.Equals("none"))//if there is error
    3. {
    4.     Debug.LogError(error);
    5.     PlayerPrefs.SetInt("isError",1);
    6.     Application.LoadLevel("MainScreen");
    7.     Debug.Log("this should not show");
    8. }
    9. else
    10. {
    11.     Debug.Log("No error");
    12. }
    13.  

    Edit: Did some more testing and if I change the name to something that is not defined in the build settings, it does give me an error, so it must at least see the command. Also I tried "MainScreen.unity" and that results in an error, so "MainScreen" must be right.

    Edit: Turns out the Update() function is only called once also. There is more detail about this in the later replies.
     
    Last edited: Nov 29, 2015
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    It seems trivial but verify that the "MainScreen" scene has been added to the build settings. File->Build Settings, then add the scene by dragging and dropping it in.
     
  3. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    It has been added already.
     
    Last edited: Nov 28, 2015
  4. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    I just tried updating Unity to the most recent version, 5.2.3f1 (the version I was using before was 5.2.2f1). Unfortunately, still no luck. If no one else has anything to say, I will report this as a bug.
     
  5. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    What's weird is that I don't seem to have this problem in the main screen. If I click a button in that screen that I've set up to load other scenes then everything works fine...
     
    Last edited: Nov 29, 2015
  6. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Did even more testing; for some reason the Update() function gets called only once. I checked and it does finish doing everything it should, so it is not like it gets stuck on any line(s) of code. Can anyone tell me why that would happen?
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you put a break point inside the if statement to see if it is being called at all?also, & I'm not positive as I've only used player prefs once, do you need to save the player prefs before you load the next scene?
     
  8. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    What do you mean by break point? I think I know what you mean, but I'm not sure how that would tell me if it's called. I do know that it's called anyway though because of the Debug.Log statement.

    As to the PlayerPrefs, the function I called is what saves them, or rather it saves a single value to the PlayerPrefs.
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I thought it set the value but you need to call playerprefs.save, check the unity documentation just in case.

    A breakpoint is a debug tool that causes the code to pause when it gets to the line you set as the break point. It lets you check that the programme is getting to that line of code, lets you check the values being used, & lets you step through the code line by line after that so you can check the values & see that it is working correctly.
     
  10. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    I just looked up PlayerPrefs.Save(), and the function does not sound necessary. The page seems to say that Unity saves the PlayerPrefs to the disk by default at certain times, and the Save() function is more of something that saves outside of those times. I have used PlayerPrefs many times in the past and haven't had a problem with them not being saved.

    Anyway, I don't think the Application.LoadLevel problem is related to the PlayerPrefs, I think it might be related to the fact that the Update() function is only being called once though.
     
  11. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Would any of these import statements affect anything having to do with update being called once and such? I know UnityEngine and System conflict in some areas, for example they both have a Random class.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.Runtime.Serialization.Formatters.Binary;
    6. using System.IO;
    7.  
     
  12. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Thing is, update is called every tick so it can't be called just once unless the object that has the script is being destroyed, the if statements etc are never valid for it to do anything, or something breaks the game before it gets back to that point. Put a debug.log in your update, outside of your if & else statement, & see if it hits it, otherwise you will need to learn how to use break points.
     
  13. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    I know that. That is why it does not make any sense. I did put a debug.log in my update outside of any other structures like if statements. In fact I put several debug.log statements. I even set up a variable to increment each time the update function ran and then debug.log 'ed the variable right at the top of the function. The whole thing consistently runs only once, and the debug.log statements at the begining and middle all run, and the debug.log statement at the very end (it is the last thing in the function) runs as well. I can tell you that I am 100% sure it only runs once in the script, no matter how supposedly "impossible" that should be.

    Edit:
    And also the object is not destroyed, or at least I don't have any code that would destroy it. The script is attached to the scene's camera.
    The code also does not "break" (or for better terms get stuck somewhere) in the Update() because the debug.log statement at the end runs perfectly well.
     
    Last edited: Nov 29, 2015
  14. ajsnarr98

    ajsnarr98

    Joined:
    Jun 15, 2015
    Posts:
    73
    Anyone else have any ideas about the Update() function problem?