2
votes

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?

EDIT: It seems like the problem is caused by a prefab I'm using, namely ALPSVR. It is a virtual reality SDK that allows you to have VR cameras independent of Unity's own VR implementation (which supports some VR devices and GearVR, but not regular Android devices).

I tried destroying the VR camera from the scene before reloading it but again it didn't solve the problem. Why would a certain GameObject inside a scene prevent Application.LoadLevel from working properly?

1
is your Time.timeScale = 0 ?JeanLuc
If you are using Unit 4.x, you need Unity Pro otherwise ALPSVR will not work.Puneet
@JeanLuc, I haven't touched the timeScale in anywhere inside the game. Would that cause a problem though?halileohalilei
@Puneet I'm using Unity 5.1.halileohalilei
yes if with a timeScale of 0 the 5 seconds would actually never pass and you method RestartLevel not invokedJeanLuc

1 Answers

0
votes

You may have success using Application.LoadLevelAsync(Application.loadedLevelName);.

If that does not work, consider creating a second blank scene. When your game level is completed, call Application.LoadLevel(Application.blankScene);, then have this script on a GameObject in that blank scene:

public class LevelLoader : MonoBehaviour
    {
      void Start()
      {
        StartCoroutine(ReloadLevel());
      }

      IEnumerator ReloadLevel()
      {
        yield return Resources.UnloadUnusedAssets();
        Application.LoadLevel(Application.loadedLevelName);
      }