0
\$\begingroup\$

I'm developing a game using Unity. The game has a single level and after the player wins/loses, I simply want to reload the same scene. Here's the code I'm using:

private void OnLevelFailed()
{
    IsGameStarted = false;
    Debug.Log("LEVEL FAILED");
    Invoke("RestartLevel",5);
}

void RestartLevel()
{
    Application.LoadLevel(Application.loadedLevelName);
}

When running this code on the editor, everything seems fine and the scene reloads as intended. However, when I build the game for Android and run it on a device (an LG G3), the game simply freezes. I used adb logcat to see the logs of any exceptions or errors if there were any but to no luck. I also suspected if the Invoke call was causing the problem and replaced it with simply the method call, but that didn't solve the problem either.

Anyone have any idea why this might happen?

\$\endgroup\$
1
\$\begingroup\$

This is the expected behaviour of LoadLevel. If you don't want your game to freeze while loading scene, try LoadLevelAsync or LoadLevelAdditive.

\$\endgroup\$
0
\$\begingroup\$

I don't know if you're still interested but I came across the same issue and solved doing a workaround.

Make a new scene, you're going to use it to load other scenes so a name of LoadingScene could do.

Inside this scene put an empty game object and attach to it a script that uses Application.LoadLevel(); inside its Awake() method, or Start() if you prefer.

Here is an example:

using UnityEngine;
using System.Collections;

public class LoaderOfScenes : MonoBehaviour {

  // Use this for initialization
  void Awake () {

    //Just load something and catch the null exception with a default scene.
    Application.LoadLevel(References.SceneToLoad == null ? 
      "ProtoMenu" : 
      References.SceneToLoad);
  }

  // Update is called once per frame
  void Update () {

  }
}

The catch is References.SceneToLoad.

It's just a static string cached inside a public static class you make.

Whenever you need to load a scene, instead of using Application.LoadLevel(); and passing as parameter the name of the scene you want to load, you assign that value to References.SceneToLoad and then load the scene that loads scenes instead.

I tried this with my builds and worked.

I don't know why.

\$\endgroup\$
0
\$\begingroup\$

This issue may be caused by other problems also.

  1. Try loading the scene with this code:

     Application.LoadLevel(Application.loadedLevel);
    

    sometimes the loaded level name gives problems if the build is a split binary.

  2. Make sure your adb log cat limit is set to over 100k (My mobile OS shoots lot of internal logs due to which unity logs just disappear form the list)

  3. Make sure you include the scene in your build.

  4. If nothing works then duplicate the scene in the asset folder and try to load the duplicate scene, and check if it gives any error.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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