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

Official 2D Roguelike: Q&A

Discussion in 'Community Learning & Teaching' started by Matthew-Schell, Feb 10, 2015.

Thread Status:
Not open for further replies.
  1. plaidmoss

    plaidmoss

    Joined:
    Dec 19, 2021
    Posts:
    3
    7 years later, I'm still using this tutorial, so thanks for writing it :D

    Probably because this is 7 years old, I'm struggling sometimes. For whoever needs to see this:

    Using Unity 2019.4.22f1, at level generation, step 2, video at 3:01, pressing play does not work and the console shows errors:
    • Problem: a lot of "already defines member" errors.
    • Problem: "type or namespace name 'BoardManager' could not be found" error.
      • Solution: add "using Completed;" to the top of BoardManager.cs and GameManager.cs
      • Youtube comment ref
     
  2. Rrawrr

    Rrawrr

    Joined:
    Sep 13, 2017
    Posts:
    3
    I tried to implemet your solution, but it didn't work for me.
    I solved it like this:
    In the MovingObject script set variable isMoving as public
    public bool isMoving;

    And in the GameManager script, in the MoveEnemies coroutine I wait for all enemies to finish moving:
    Code (CSharp):
    1. private IEnumerator MoveEnemiesCoroutine()
    2.     {
    3.         isEnemiesMoving = true;
    4.         yield return new WaitForSeconds(turnDelay);
    5.  
    6.         if (enemies.Count == 0)
    7.         {
    8.             yield return new WaitForSeconds(turnDelay);
    9.         }
    10.  
    11.         for (int i = 0; i < enemies.Count; i++)
    12.         {
    13.             enemies[i].MoveEnemy();
    14.             yield return new WaitForSeconds(enemies[i].moveTime);
    15.  
    16.             while (enemies[i].isMoving)
    17.             {
    18.                 yield return null;
    19.             }
    20.         }
    21.         isEnemiesMoving = false;
    22.         isPlayerTurn = true;
    23.     }
     
  3. Rrawrr

    Rrawrr

    Joined:
    Sep 13, 2017
    Posts:
    3
    Also with this thing (weird level count and double game initialization)
    The way I solved it is:
    Added new empty scene 'preloader' with a script which loads your game scene. In my case it's named 'app'
    Code (CSharp):
    1. public class Preloader : MonoBehaviour
    2. {
    3.     void Awake()
    4.     {
    5.         SceneManager.LoadScene("app");
    6.     }
    7. }
    And in the GameManager I removed InitGame game method from Awake. Its left only in the OnSceneLoaded method
     
    Last edited: Jan 8, 2022
  4. MrSkitson

    MrSkitson

    Joined:
    Sep 25, 2021
    Posts:
    1
    Hi everyone! I have problem in step 10-11.
    When i finished Enemy script I have an error in Unity . What I can do with it?
    Assets/MyWay/Scripts/Enemy.cs(23,34): error CS1061: 'GameManager' does not contain a definition for 'AddEnemyToList' and no accessible extension method 'AddEnemyToList' accepting a first argument of type 'GameManager' could be found (are you missing a using directive or an assembly reference?)
     

    Attached Files:

  5. sumiv

    sumiv

    Joined:
    Oct 21, 2021
    Posts:
    2
    My player is unable to move more than one step because I have two boards loading when I start the game and two InitGame() in my GameManager.cs. I tried implementing the solutions that people mentioned earlier on this forum (adding the
    OnLevelFinishedLoading() and level increment) but commenting out the InitGame() in Awake() gives me a completely black screen with no boards loaded and commenting out the Awake() in OnLevelFinishedLoading still loads two boards. My game also doesn't get rid of the "Day 1" levelImage by itself, I have to manually uncheck it on Unity. Does anybody have any idea on how I could fix these issues?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6.  
    7.     //"singleton" - only one instance in the game at a time
    8.     public class GameManager : MonoBehaviour
    9.     {
    10.         public float levelStartDelay = 2f;
    11.         public float turnDelay = .1f;
    12.         public int playerFoodPoints = 100;
    13.         public static GameManager instance = null; //static "instance" allows it to be accessed by any other script
    14.         [HideInInspector] public bool playersTurn = true;
    15.  
    16.         private Text levelText;
    17.         private GameObject levelImage;
    18.         private BoardManager boardScript;
    19.         private int level = 1;
    20.         private List<Enemy> enemies;
    21.         private bool enemiesMoving;
    22.         private bool doingSetup = true;
    23.  
    24.        
    25.        
    26.         void Awake()
    27.         {
    28.             //check if this is the first time we run this
    29.             if(instance == null)
    30.             {
    31.                 //if not we set instance to this game
    32.                 instance = this;
    33.  
    34.             } else if (instance != this)
    35.             {
    36.                 Destroy(gameObject);
    37.             }
    38.             //set this to not be dstroyed when reloading scene
    39.             DontDestroyOnLoad(gameObject);
    40.  
    41.             enemies = new List<Enemy>();
    42.  
    43.             //taking boardScript (reference) and attaching it to BoardManager script
    44.             boardScript = GetComponent<BoardManager>();
    45.  
    46.             //InitGame(); //commenting out -> black screen
    47.         }
    48.  
    49.         // private void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
    50.         // {
    51.         //     //Add one to our level number.
    52.         //     level++;
    53.         //     //Call InitGame to initialize our level.
    54.         //     InitGame();
    55.         // }
    56.  
    57.         //this is called only once, and the paramter tell it to be called only after the scene was loaded
    58.         //(otherwise, our Scene Load callback would be called the very first load, and we don't want that)
    59.         // [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    60.         // static public void CallbackInitialization()
    61.         // {
    62.         //     //register the callback to be called everytime the scene is loaded
    63.         //     SceneManager.sceneLoaded += OnSceneLoaded;
    64.         // }
    65.  
    66.         // static private void OnSceneLoaded(Scene arg0, LoadSceneMode arg1)
    67.         // {
    68.         //     instance.level++;
    69.         //     instance.InitGame();
    70.         // }
    71.  
    72.         private void OnLevelFinishedLoading (Scene scene, LoadSceneMode mode){
    73.             if (level > 1){
    74.                 InitGame(); //commenting out -> still two boards
    75.             }
    76.             level++;
    77.         }
    78.  
    79.         private void OnEnable() {
    80.             SceneManager.sceneLoaded += OnLevelFinishedLoading;
    81.         }
    82.  
    83.         private void onDisable(){
    84.             SceneManager.sceneLoaded-=OnLevelFinishedLoading;
    85.         }
    86.  
    87.         //initialize the game for each level
    88.         void InitGame()
    89.         {
    90.             //While doingSetup is true the player can't move, prevent player from moving while title card is up.
    91.             doingSetup = true;
    92.            
    93.             //Get a reference to our image LevelImage by finding it by name.
    94.             levelImage = GameObject.Find("LevelImage");
    95.            
    96.             //Get a reference to our text LevelText's text component by finding it by name and calling GetComponent.
    97.             levelText = GameObject.Find("LevelText").GetComponent<Text>();
    98.            
    99.             //Set the text of levelText to the string "Day" and append the current level number.
    100.             levelText.text = "Day " + level;
    101.            
    102.             //Set levelImage to active blocking player's view of the game board during setup.
    103.             levelImage.SetActive(true);
    104.            
    105.             //Call the HideLevelImage function with a delay in seconds of levelStartDelay.
    106.             Invoke("HideLevelImage", levelStartDelay);
    107.            
    108.             //Clear any Enemy objects in our List to prepare for next level.
    109.             enemies.Clear();
    110.            
    111.             //Call the SetupScene function of the BoardManager script, pass it current level number.
    112.             boardScript.SetupScene(level);
    113.         }
    114.  
    115.         void HideLevelImage()
    116.         {
    117.             levelImage.SetActive(false);
    118.             doingSetup = false;
    119.         }
    120.  
    121.         void Update ()
    122.         {
    123.             if(playersTurn || enemiesMoving || doingSetup)
    124.                 return;
    125.  
    126.             StartCoroutine (MoveEnemies ());
    127.         }
    128.  
    129.         //Call this to add the passed in Enemy to the List of Enemy objects.
    130.         public void AddEnemyToList(Enemy script)
    131.         {
    132.             //Add Enemy to List enemies.
    133.             enemies.Add(script);
    134.         }
    135.  
    136.         public void GameOver()
    137.         {
    138.             levelText.text = "After "+level+" days, you starved.";
    139.             levelImage.SetActive(true);
    140.             enabled = false;
    141.         }
    142.  
    143.          //Coroutine to move enemies in sequence.
    144.         IEnumerator MoveEnemies()
    145.         {
    146.             enemiesMoving = true;
    147.             yield return new WaitForSeconds(turnDelay);
    148.             if (enemies.Count == 0)
    149.             {
    150.                 yield return new WaitForSeconds(turnDelay);
    151.             }
    152.  
    153.             for (int i = 0; i < enemies.Count; i++)
    154.             {
    155.                 enemies[i].MoveEnemy ();
    156.                 yield return new WaitForSeconds(enemies[i].moveTime);
    157.             }
    158.  
    159.             playersTurn = true;
    160.             enemiesMoving = false;
    161.         }
    162.     }
    163.  
    164.  
    165.  
    Screen Shot 2022-03-17 at 11.06.31 AM.png
     
  6. Kassandra79

    Kassandra79

    Joined:
    Feb 12, 2022
    Posts:
    1
    Hi,
    I just managed to make my game work by following the tutorial and doing research on the things that didn't work. There were quite a number of issues in the new version of Unity.
    However, I do have three issues I do not understand:
    1. Sometimes when the game loads, the food count goes down automatically and I have a hard time moving the player. Restarting helps.
    2. I tried building for WebGL to upload the game to Simmer. But the game that is created does not work. Everything seems blurry and the player doesn't move. Not sure if this is because I added the touch controls from video 14? I thought that part was disabled when I switch back to Windows platform. I did notice though, that the #if and #else statements from the videos didn't become pink but remained blue, so maybe I broke something?
    3. Is the player supposed to walk on the same tile the enemy is on? They overlap in my game.

    My code is here: https://github.com/kassandra79/roguelike
     
    Last edited: Aug 19, 2022
  7. link_108

    link_108

    Joined:
    Sep 20, 2022
    Posts:
    1
    I'm using unity 5.3 and I'm seeing this behavior currently.
     
Thread Status:
Not open for further replies.