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. Dismiss Notice

Scenemanager.LoadScene(int index) works in editor only - total loss of function in builds

Discussion in 'Scripting' started by ChemPro, Aug 31, 2016.

  1. ChemPro

    ChemPro

    Joined:
    Nov 10, 2015
    Posts:
    8
    I've spent the afternoon trying to figure this out and I'm out of ideas.

    I have a title screen in my game that has a button ("New Game"). This button's code:

    Code (CSharp):
    1. public void ButtonClick (int ID)
    2.     {
    3.         GameData.DataLord.SetLoadType(Constants.NEW_GAME);
    4.         SceneManager.LoadScene(Constants.INTRO_SCENE_ID);
    5.     }

    The first line is a donotdestroy object. The second line is a constant with a value of '3', matching the build settings index of the scene I want to load:
    upload_2016-8-30_19-2-33.png


    This works perfectly fine in the editor - however in both standalone AND android builds the next scene does not load. I've confirmed that the button fires correctly etc it is the loadscene function that is having some problem. I have included using UnityEngine.Scenemanagement in the script and again, everything compiles and runs in the editor just fine. I've tried replacing the constant value with just a '3' and the scene name. Each time it works fine in the editor but does nothing in the build itself.

    Anyone know why this might be happening? Thanks in advance for any help.
     

    Attached Files:

  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I'm having difficulty imagining what could be doing this, and I can't find any references on the net to this kind of problem, so I'll just give you some ideas for hitting it with a rock and gathering more information.

    First, if you aren't using the latest major Unity build (5.4), I would upgrade (or do a new install to test it, if you like). If you know how to enable and view error logs for Android you might want to try that, see if an error occurs, and copy over the results here if it does (usually an error will crash the program, but not always). I might try checking to see if you've got the stripping level to "micro mscorlib" (typically a bad idea) or try building for an earlier version of Android (lately I've been aiming for Jelly Bean most of the time, 4.1). Try activating something visible on the screen when the button is pressed and see if it's even getting to this function, try swapping around the two method calls here and see if it loads the new scene if the command comes first (even if it immediately crashes it by doing things out of order, that'll tell you something).
     
  3. ChemPro

    ChemPro

    Joined:
    Nov 10, 2015
    Posts:
    8
    Hey Thanks for chiming in! Sadly, I've exhausted quite a bit of what you mentioned. The error logs do show:

    NullReferenceException: Object reference not set to an instance of an object
    at TitleScreenManager.LoadScene () [0x00000] in <filename unknown>:0
    at TitleScreenManager.Update () [0x00000] in <filename unknown>:0

    The problem is everybody who seems to have this issue falls back to not setting it up correctly on the build settings - but mine seem to be clearly set. I tried building for the earliest Android version and a few later with no success either. I have confirmed the button is firing fully and that it is the loadscene function itself causing an issue - but why I have no idea. The stripping level is currently disabled. I'm running 5.3.5f1 - the program says this is the latest version though?
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Another thing you could try is add another Scene. Just put one button in this scene, and have you code try to load that scene. If it works then you know something specifically in Prologue scene is messing up. Add Objects from the Prologue scene one by one till it breaks. tedious but could be the fastest way to track it down
     
  5. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    What does TitleScreenManager.LoadScene look like? The error's not coming from ButtonClick.
     
  6. ChemPro

    ChemPro

    Joined:
    Nov 10, 2015
    Posts:
    8
    Code (CSharp):
    1.     void LoadScene()
    2.     {
    3.         switch (userChoice)
    4.         {
    5.             case 1:
    6.                 {
    7.                     GameData.DataLord.SetLoadType(Constants.NEW_GAME);
    8.                     SceneManager.LoadScene(Constants.INTRO_SCENE_ID);
    9.                     break;
    10.                 }
    11.             case 0:
    12.                 {
    13.                     GameData.DataLord.SetLoadType(Constants.LOAD_GAME);
    14.                     SceneManager.LoadScene(Constants.TREE_SCENE_ID);
    15.                     break;
    16.                 }
    17.         }
    18.     }

    In the original design the button sets a var which is then used in the switch above, hence it showing LoadScene() causing the error. In the effort to troubleshoot though I truncated it to just run those two lines whenever any button was pressed.
     
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    So if you edited the Button to NOT call TitleManager.LoadScene, what error are you getting now. From the original ButtonHandler you posted, it shouldn't be calling Titlemanager at all. It just loads the scene directly itself?
     
    Last edited: Aug 31, 2016
  8. ChemPro

    ChemPro

    Joined:
    Nov 10, 2015
    Posts:
    8

    So I did this and a completely empty scene with just a single button is able to load any other scene just fine. The problem must be within the TitleScreenManager script. I'll start to chop it up and report with my findings. Thanks for your help!
     
  9. ChemPro

    ChemPro

    Joined:
    Nov 10, 2015
    Posts:
    8
    OK - So an update.

    First, I have to come clean and say I was off. The issue is NOT within the scenemanagement function. It just really appeared that way. By turning off objects I found the culprit/issue was within my GameData class as it tried to open XML files during its Awake(). By calling to GameData (null due to the exception occurring during awake) every time just before the scenetransition call none of it was going through. Not sure why the XML files are working fine in editor but not accessible in the final build - more fun for tomorrow I suppose. Lesson learned!

    Anyway - thanks a lot to everyone for the ideas troubleshooting, it definitely helped me narrow it down quickly. Sorry for the red herring post title! Ill need to figure out a reliable way to see log/error data from the live-on-phone build asap if I'm going to go further with this.

    -CP