Search Unity

2d tutorial roguelike issues

Discussion in '2D' started by WaWaRasputin, Oct 7, 2016.

  1. WaWaRasputin

    WaWaRasputin

    Joined:
    Oct 7, 2016
    Posts:
    1
    Hi there.

    Solved a problem inside the tutorial 2D roguelike stuff.

    Problem is, if you follow the current tutorial, you'll notice that your character only move once and once completed, the game start at day 2, then jumps at day 4 where you can only make 1 single move before gameOver hits you.

    Here's why :

    Loader.cs is instantiated before the rest of the mainScene was fully loaded. Hence GameManager is already instantiated before the Day 1 scene throws its "Level Finish Loading" event.

    This means GameManager.cs calls its own "OnLevelFinishedLoading()" after Level 1 is loaded. Which makes the game directly jump to Day 2.

    When finishing day 2 through the Exit, the scene is reloaded, hence Loader is reloaded which recreate the same "jump one Level" with one twist : Player object now goes through OnDisable() twice instead of once.
    Player starve after one single move because one Player instance is being loaded and disabled without going throught the "Start()" method. It probably only goes through Awake() : It therefore never takes the food Count inside of the singleton GameManager before it reach the "OnDisable()" part. This part override the GameManager food count with the default value of Player.cs script which is unspecified, and therefore 0. Hence, next move is GameOver condition.

    To solve it :
    In Loader.cs remove the instantiate GameManager part from Awake() and replace it with this :

    Code (csharp):
    1.  
    2.  
    3.     void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
    4.         {
    5.             if (GameManager.instance == null)
    6.                 Instantiate(gameManager);
    7.         }
    8.         void OnEnable()
    9.         {
    10.             //Tell our ‘OnLevelFinishedLoading’ function to start listening for a scene change event as soon as this script is enabled.
    11.             SceneManager.sceneLoaded += OnLevelFinishedLoading;
    12.         }
    13.    
    14.         void OnDisable()
    15.         {
    16.             //Tell our ‘OnLevelFinishedLoading’ function to stop listening for a scene change event as soon as this script is disabled.
    17.             //Remember to always have an unsubscription for every delegate you subscribe to!
    18.             SceneManager.sceneLoaded -= OnLevelFinishedLoading;
    19.         }
    20.  
    Now Loader.cs won't instantiate GameManager before the Scene is already loaded, and GameManager won't skip levels nor create the situation where Player.cs override GameManager.food to 0

    You cannot solve this by using Start() instead of Awake() in Loader.cs because Player needs the GameManager singleton to be instantiated before being loaded by the Engine : this is why Player and MovingObject are initialized inside Start() method and not inside Awake().

    Hope it will get fixed in the tutorial website. Good debugging tutorial ;p

    PS: it's 2A.M and I've tried to find a way to contact people of uniteam to report this but didn't found it.
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    I spent a long time myself on that tutorial too trying to get it working. To be honest i'm glad its broken because i learnt a lot debugging it.
     
  3. Genalx

    Genalx

    Joined:
    Nov 18, 2017
    Posts:
    3
    This helped a TON!
    These Unity version differences can throw me for a loop sometimes, and being new to Unity in general, I'm thankful there are geniuses like you out there. XD

    Thank you!
     
    robertdmoores likes this.