Fix for 2D Roguelike no restart and jumping scenes after build?
Has anyone have a solution to this problem? I am using the latest unity program and i go to build for android and it says that the Application.LoadLevel is obsolete and that it should be SceneManager.LoadScene.
After I build for android the game loads day 2 then jumps to day 4 then one step i die.
I dont know how to write this code to make the game restart with the SceneManager could someone explain pls?
game also will not restart after player dies. I have not changed the code of the tutorial and still it does not reload.
2D Roguelike tutorial
Answer by davidrynn · Oct 14, 2016 at 06:15 AM
A couple of things... In your hierarchy, make sure you only have the Player, Canvas (and EventSystem) and Main Camera.
I had the GameManager object there which was adding an extra instance, I think. I made sure I had converted it into a prefab and deleted it from the hierarchy.
Also, on the Main Camera, make sure you added the Loader script and added the appropriate prefabs.
But here's the more complicated part: Since Unity 5.4+ OnLevelWasLoaded has been deprecated. Upgrade guide recommends an incomplete solution. The full solution is so (including the bool):
At the top of your code write :
using UnityEngine.SceneManagement;
then in the GameManager class:
private bool firstRun = true;
void OnEnable()
{
//Tell our 'OnLevelFinishedLoading' function to start listening for a scene change as soon as this script is enabled.
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
void OnDisable()
{
//Tell our 'OnLevelFinishedLoading' function to stop listening for a scene change as soon as this script is disabled. Remember to always have an unsubscription for every delegate you subscribe to!
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
{
if (firstRun)
{
firstRun = false;
return;
}
level++;
InitGame();
}
We have to include the bool because we don't want the level to be incremented on the first run through.
I had the boards of day 1 and day 2 overlapping when launching the game. When I was reaching the exit, it jumped to day 4 and had the same overlapping problem. Your code fixed all of that! Save my day, thank you!
superb! not sure how I missed all the code to do with the firstRun bool variable - but this was missing and adding it fixed this for me!
Thank you very much! +1
I had a similar problem:
The level number was initializing to 2 and then skipping to 4. The player's food count dropped to 0 after the first level.
I added the bool like @davidrynn suggested and now my game if functioning correctly. Can someone please explain why? I'm trying to figure out how adding the firstRun bool stopped a second instance of the game manager from happening (if that's even what it did).
Answer by Wrymnn · Feb 08, 2016 at 08:29 AM
Make sure you use this library in your script where you work with SceneManager
using UnityEngine.SceneManagement;
Now, when you want to restart particular level from scratch, you can use
SceneManager.LoadLevel("LevelName");
Try not to use indexes, since level indexes depend very much on what order are your levels/scenes in Build Settings.
So, when you player dies, just load the scene the player has died in. It should get reloaded.
The loading of the scene seems to work properly but the jumping behavior is still in place - it seems that the $$anonymous$$ethod OnLevelWasLoaded is called a second time - in the build version. not sure why that is happening at the moment
Answer by archdruidardanos · Feb 18, 2016 at 03:25 PM
After searching for another while i found the a solution in another place in the forum. Honestly i have no idea why this help or what is the difference between the builds and the unity editor but this helped the fix the built version. And it was good to see that my earlier estimations had been true.
In GameManager.cs
#if !UNITY_EDITOR static bool firstRun = true; #endif void OnLevelWasLoaded(int index) { #if !UNTIY_EDITOR %|-723459626_1|% { firstRun = false; %|1221298868_5|% } #endif }
Answer by chrisbird93 · Sep 03, 2018 at 01:33 AM
In the Awake function of GameManager you need to either delete or comment out 'InitGame();' function, as this is now getting called in the 'OnLevelFinishedLoading' function, which in turn is called once the scene is loaded.
This is definitely the answer although it's strange because the InitGame() function shouldn't be getting called since any new GameManagers are 'Destroyed' during the Awake() function if they are not the same as the current instance (singleton) ... Maybe the Awake function is continuing to be called even though we have called 'Destroy' on the object that is calling the script?
I'm not sure, but this should fix your problem at least.
(P.S. you may find that you are starting on Level 2 instead of level 1. To fix this, simply change your 'int level' to '0' instead of 1 in your GameManager script.
Chris.
Your answer
Follow this Question
Related Questions
Adding game end to infinite runner union 0 Answers
how to add picked up items in gui? 1 Answer
Can someone help me make a simple 2d endless runner script? 0 Answers
stop player from moving after button released. 0 Answers
Sprite help 0 Answers