Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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. Matthew-Schell

    Matthew-Schell

    Joined:
    Oct 1, 2014
    Posts:
    238
    Take a look at the documentation here:
    https://docs.unity3d.com/ScriptReference/Serializable.html

    Basically, since Count is a C# class not a MonoBehaviour we are using Serializable to allow it's properties to appear in the inspector.
     
  2. nfantone

    nfantone

    Joined:
    Sep 15, 2016
    Posts:
    3
    Hi all!

    I just wanted to pop in and comment to everybody that may be interested, that I have set up a public Github repository where you'll find the complete 2D Roguelike project updated to work with latest Unity Beta (5.5.0b3, at the time of this writing). This is not only a solution built after following the tutorial, but also a showcase of practices to follow if you'd like to create your own open source Unity game. It features:
    • Git LFS support for hosting large media files.
    • Travis CI integration to build your game binaries.
    • Uploading of artifacts to a private Amazon S3 bucket.
    • Usage of UnityYAMLMerge tool to resolve merge conflicts.
    • Complete .gitignore and .gitattributes files already defined.
    Read the README.md for instructions on how to configure your local environment before playing around.

    Again, repo is at https://github.com/nfantone/2d-roguelike.

    Thanks!
     
    n_krmbv likes this.
  3. justsoelmo

    justsoelmo

    Joined:
    Sep 16, 2016
    Posts:
    5
    Alright, so I'm having some issues that I've tried researching, and have not found any solutions for. In the script from the 9th video "Writing the Player", every method with "protected override" at the beginning yields a CS0115 Error ('Player.MethodName()' is marked as an override but no suitable method found to override). I've spent two hours on this, to no avail. And this is overdue for a class that's ending, so there's quite a bit of a time crunch. Can someone please help? Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class Player : MonoBehaviour {
    6.  
    7.     public int wallDamage = 1;
    8.     public int pointsPerFood = 10;
    9.     public int pointsPerSoda = 20;
    10.     public float restartLevelDelay = 1f;
    11.  
    12.     private Animator animator;
    13.     private int food;
    14.    
    15.     protected override void Start () {
    16.         animator = GetComponent<Animator>();
    17.         food = GameManager.instance.playerFoodPoints;
    18.  
    19.         base.Start();
    20.     }
    21.  
    22.     public void OnDisable()
    23.     {
    24.         GameManager.instance.playerFoodPoints = food;
    25.     }
    26.  
    27.     protected override void AttemptMove<T>(int xDir, int yDir)
    28.     {
    29.         food--;
    30.  
    31.         base.AttemptMove<T>(xDir, yDir);
    32.  
    33.         RaycastHit2D hit;
    34.  
    35.         CheckIfGameOver();
    36.  
    37.         GameManager.instance.playersTurn = false;
    38.     }
    39.  
    40.     private void OnTriggerEnter2D(Collider2D other)
    41.     {
    42.         if (other.tag == "Exit")
    43.         {
    44.             Invoke("Restart", restartLevelDelay);
    45.             enabled = false;
    46.         }
    47.         else if (other.tag == "Food")
    48.         {
    49.             food += pointsPerFood;
    50.             other.gameObject.SetActive(false);
    51.         }
    52.         else if (other.tag == "Soda")
    53.         {
    54.             food += pointsPerSoda;
    55.             other.gameObject.SetActive(false);
    56.         }
    57.     }
    58.  
    59.     protected override void OnCantMove<T>(T component)
    60.     {
    61.         Wall hitWall = component as Wall;
    62.         hitWall.DamageWall(wallDamage);
    63.         animator.SetTrigger("playerChop");
    64.     }
    65.  
    66.     private void Restart()
    67.     {
    68.         SceneManager.LoadScene(0);
    69.     }
    70.  
    71.     void Update () {
    72.         if (!GameManager.instance.playersTurn) return;
    73.  
    74.         int horizontal = 0;
    75.         int vertical = 0;
    76.  
    77.         horizontal = (int)(Input.GetAxisRaw("Horizontal"));
    78.         vertical = (int)(Input.GetAxisRaw("Vertical"));
    79.  
    80.         if (horizontal != 0)
    81.         {
    82.             vertical = 0;
    83.         }
    84.  
    85.         if (horizontal != 0 || vertical != 0)
    86.         {
    87.             AttemptMove<Wall>(horizontal, vertical);
    88.         }
    89.  
    90.  
    91.     }
    92.  
    93.     public void LoseFood(int loss)
    94.     {
    95.         animator.SetTrigger("playerHit");
    96.  
    97.         food -= loss;
    98.  
    99.         CheckIfGameOver();
    100.     }
    101.  
    102.     private void CheckIfGameOver()
    103.     {
    104.         if (food <= 0)
    105.         {
    106.             GameManager.instance.GameOver();
    107.         }
    108.     }
    109. }
    Thanks for any help you can lend.
     
  4. justsoelmo

    justsoelmo

    Joined:
    Sep 16, 2016
    Posts:
    5
    I think we're having the same problem. I'm sorry to say I don't have an answer. It's good to know I've found someone else having the same issue, though. None of my research has yielded any results on this front, sadly :-/
     
  5. Aerin41

    Aerin41

    Joined:
    Dec 24, 2014
    Posts:
    14
    You need to have the Player class extend MovingObject, not MonoBehaviour.

    MonoBehaviour doesn't have the functions OnCantMove, AttemptMove. The MovingObject class has these protected functions and marks them as virtual so they can be overriden. I believe Start is also marked virtual in the MovingObject class so it can be overriden as well.
     
    glipR and Matthew-Schell like this.
  6. farvind

    farvind

    Joined:
    Sep 15, 2016
    Posts:
    3
    Can anyone tell me how to restart the game ...
     
  7. justsoelmo

    justsoelmo

    Joined:
    Sep 16, 2016
    Posts:
    5
    Ugh. I figured it was something super simple and obvious like that, because no one else was having this issue. Thanks!
     
  8. Matthew-Schell

    Matthew-Schell

    Joined:
    Oct 1, 2014
    Posts:
    238
    I haven't tried in 5.5 yet, did you notice any significant changes that needed to be made? I just published an upgrade guide for the change related to SceneManagement for 5.3 and later.
     
  9. farvind

    farvind

    Joined:
    Sep 15, 2016
    Posts:
    3
    So i worked on Restarting the Game. I have two scenes. Both Scenes Works fine individually but when its time to build the game, the game stucks when the menu scene pops up despite destroying GameMAnager. I have gone through the forum and tried every bit of help and it seems like it helped. Btw unity version is 5.3.6f1.Please help me out to fix this.Thanks in advance
     
  10. nfantone

    nfantone

    Joined:
    Sep 15, 2016
    Posts:
    3
    None - except for the one you just mentioned. Or at least nothing that I can recall as being particular hard to elucidate.
     
    Matthew-Schell likes this.
  11. Sky1988

    Sky1988

    Joined:
    Aug 25, 2016
    Posts:
    9
    i understand now, thank you
     
    Matthew-Schell likes this.
  12. meowwolf

    meowwolf

    Joined:
    Sep 19, 2016
    Posts:
    1
    My GameManager keeps getting destroyed.... and I can only play level one. After that it skips to level3 and no food is left. I followed through the tutorial but probably I missed something. Since OnLevelWasLoaded was deprecated, I use the suggested code in the upgradePDFguide with the OnEnable/OnDisable/OnLevelFinishedLoading
    1. GameManager gameobject created with both BoardManager and Gamemanager scripts setup.
    2. Make Gamemanager prefab
    3. Drag GameManager prefab to loader script for the Main Camaera
    4. Runs the game, but the GameManager in the loader scripts becomes "missing" during the runtime...
    5. Drag GameManager prefab to the heirarchy does not help... (thought i give this a try)


    sidenote.. I have public variables (restart/exit buttons) setup in my GameManager Scripts but the restart button is "missing" when I created the GameManager prefab... I did specify the onclick action for them but they are all missing when they become prefab....??

    Can anyone help me? Thanks.
     
  13. Aerin41

    Aerin41

    Joined:
    Dec 24, 2014
    Posts:
    14
    The only reason I can think of that the GameManager in the loader script would go missing at runtime, is if you're dragging the prefab instance from the Hierarchy to the script, and then deleting the instance in the Hierarchy. You sure that's not happening?

    I'm not sure I understand. The game gets stuck as soon as the menu pops up, or after it goes away? If the GameManager is destroyed then the game won't work.
     
  14. chip5

    chip5

    Joined:
    Sep 20, 2016
    Posts:
    1
    The problems people are having with seeing things happen twice that should only happen once in the debugger is because there are two boards being loaded. This is also the reason you will die after a level transition, one board gets the player food count, the other one misses it. There are two boards because InitGame() is called twice in the example GameManager class, once in Awake() and once in OnLevelWasLoaded() (aka OnlevelFinishedLoading() in v5.4+). Remove the call to InitGame() from Awake() and the game will work.

    You can see this bug in the example code here (https://unity3d.com/learn/tutorials...al/adding-ui-level-transitions?playlist=17150).
     
  15. farvind

    farvind

    Joined:
    Sep 15, 2016
    Posts:
    3
    ha ha ... I worked out a bit and it worked :D. I am really not sure what the bug was but none the less ,the game runs .
     
  16. justsoelmo

    justsoelmo

    Joined:
    Sep 16, 2016
    Posts:
    5
    So, at the point in video 11 where he tests whether or not the enemy attacks, I get nothing. I can't get him to attack, no matter how many times I click the condition. Here's my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : MovingObject
    5. {
    6.     public int playerDamage;
    7.  
    8.     private Animator animator;
    9.     private Transform target;
    10.     private bool skipMove;
    11.  
    12.     protected override void Start()
    13.     {
    14.         animator = GetComponent<Animator>();
    15.  
    16.         target = GameObject.FindGameObjectWithTag("Player").transform;
    17.  
    18.         base.Start();
    19.     }
    20.  
    21.     protected override void AttemptMove<T>(int xDir, int yDir)
    22.     {
    23.         if (skipMove)
    24.         {
    25.             skipMove = false;
    26.             return;
    27.  
    28.         }
    29.  
    30.         base.AttemptMove<T>(xDir, yDir);
    31.  
    32.         skipMove = true;
    33.     }
    34.  
    35.     public void MoveEnemy()
    36.     {
    37.         int xDir = 0;
    38.         int yDir = 0;
    39.  
    40.         if (Mathf.Abs(target.position.x - transform.position.x) < float.Epsilon)
    41.             yDir = target.position.y > transform.position.y ? 1 : -1;
    42.         else
    43.             xDir = target.position.x > transform.position.x ? 1 : -1;
    44.  
    45.         AttemptMove<Player>(xDir, yDir);
    46.     }
    47.  
    48.     protected override void OnCantMove<T>(T component)
    49.     {
    50.         Player hitPlayer = component as Player;
    51.  
    52.         hitPlayer.LoseFood(playerDamage);
    53.  
    54.         animator.SetTrigger("enemyAttack");
    55.     }
    56. }
    Does anyone have any ideas?
     
  17. Flagusco

    Flagusco

    Joined:
    Sep 22, 2016
    Posts:
    4
    Instead of if (canMove && hitComponent != null)
    i'ts if(!canMove && hitComponent != null)

    :)
     
  18. Flagusco

    Flagusco

    Joined:
    Sep 22, 2016
    Posts:
    4
    line 22 in your code should be Destroy(gameObject);
    Check your Animation Controller folder and open Enemy1. The trigger is probably called EnemyAttack not enemyAttack. Note the Capital "E". Hope that helps.
     
  19. Flagusco

    Flagusco

    Joined:
    Sep 22, 2016
    Posts:
    4
    Has anyone wrapped the main game with an intro screen with start/quit buttons and a game over screen with a restart button? I've hooked these up but reloading the intro scene doesn't kill the existing instance of gameObject so the game level persists with 0 food and then the player dies...any ideas are welcome.
     
  20. Loim

    Loim

    Joined:
    Sep 15, 2016
    Posts:
    2
    I am feeling a bit daft. For the first step of the tutorial, I was able to get the Player and Enemy1 to spawn and animate but Enemy2 isn't spawning. I believe that I did everything right as far as the tutorial video goes, any other suggestions? Something with the Controller Override? Also, how do you move the Animation tab to see all the stares on the screen at once? My screen seems to have moved to the right, leaving everything to the left and I can only see "Enemy1Idle" and "Enemy1Attack" for instance.

    Thanks for your time and help.
     
  21. FBones

    FBones

    Joined:
    Aug 28, 2016
    Posts:
    73
    Where in this script does the Player get instantiated? I cannot find it anywhere.

    Similarly, how is persistence across levels handled? I would have expected to see the player script update game manager at the end of the level with his new food count, and I also would expect to see the player either re-enabled and moved at the beginning of the scene or reinstantiated.
     
  22. Flagusco

    Flagusco

    Joined:
    Sep 22, 2016
    Posts:
    4
    The Player prefab has to be in the hierarchy so there is no need to instantiate. The player inherits movement from the MovingObject script.
     
  23. AthenaHunter

    AthenaHunter

    Joined:
    Sep 30, 2016
    Posts:
    1
    This tutorial was wonderful in explaining and using many of the intermediate concepts outlined in the other tutorials. Thank you for creating and providing helpful responses to questions on this forum. I think I have been able to understand all of this tutorial except for a few things and any help anyone can provide in clearing these things up for me would be greatly appreciated.

    1. Why does it have to be it GameManager.instance.AddEnemyToList(this); and not GameManager.AddEnemyToList(this) in the Enemy script?

    2. Does this code ever create more than one instance of GameManager or SoundManager? It looks like Loader will only create one instance. Is the singularity just a precaution incase some other new script Instantiates gamemanager.

    3. In the SoundManager and GameManager scripts why is gameObject lowercase in the code // Destroy(gameObject); // when it is not defined anywhere? Does it default to mean this gameObject?

    4. Is “this” the script, an instance of the script, or an object?

    5. It seems the bulk of my questions revolve around the singularity process. Can someone please step me through what is going on?

    I am new to OOP and Unity so the more information the better. Thanks for your help.
     
  24. Kenmairu

    Kenmairu

    Joined:
    Oct 6, 2016
    Posts:
    1
    Ive just began this tutorial for a class I am currently taking, and ive combed through all of my coding and I am 99% sure it is no different then that shown in the videos. But for some reason when my player walks over a food, soda or exit tile they don't do anything or interact in any way. The food tiles don't disappear and the game doesn't restart upon exit
     
  25. ArcticFire421

    ArcticFire421

    Joined:
    Oct 7, 2016
    Posts:
    1
    I am doing this tutorial for a tutoring and I ran into an issue that my tutor and others can not make sense of. I am using SceneManager.sceneLoaded and I am getting this error:`UnityEngine.SceneManagement.SceneManager' does not contain a definition for `sceneLoaded'
     

    Attached Files:

  26. Deleted User

    Deleted User

    Guest

    Hi,

    I just downloaded the project into Unity 5.5.0b6 and got this error message:
    I didn't import the "Completed" and "Project Settings" directories.

    Edit: I had to insert a space between the letters : and D in the package name because they make a smiley.
     
    Last edited by a moderator: Oct 8, 2016
  27. badbobbyk

    badbobbyk

    Joined:
    Oct 10, 2016
    Posts:
    1
    Out of curiousity, is the information in these tutorials available in a non-video format? The content is excellent, but viewing it as a video is a tedious and clunky experience.
     
  28. Deleted User

    Deleted User

    Guest

    You'll find the transcripts under the videos.
     
  29. SubThoRed

    SubThoRed

    Joined:
    Oct 12, 2016
    Posts:
    1
    Well.. First of all sorry for my English. It's not my native language :(
    I have a problem with Enemy.cs.
    MonoDevelop says that i have error in line with
    Code (CSharp):
    1. base.Attemptmove (xDir, yDir);
    Text of error: "error CS0411: The type arguments for method `MovingObject.Attemptmove<T>(int, int)' cannot be inferred from the usage. Try specifying the type arguments explicitly"
    As i think, script can't get the integer value from MovingObject.cs. And i dont understand why. First i cheked all scripts with examples in Learning section of site. Triple checked..
    P.S. Hope you understand me, cause it so hard for me to express what i mean in English.

    After some searching i found that same line with same code works well in Player.cs. Now I totally disappointed :eek:

    After another 30 minutes, i solve a problem. I change
    Code (CSharp):
    1. public class Player : MovingObject
    to
    Code (CSharp):
    1. public class PlayerScript : MovingObject
    Movement work correctly, and i can hit walls. But Player dont interact with food and enemies.
    Lets find out if I can fix it.

    And now I fix problem with interaction. Just change "onTriggerEnter2D" to "OnTriggerEnter2D".
    Here is another bug. Player stop moving after pickup a food..
    If someone ask "Why you write all of this? If you did it yourself." Answer is - if anyone would have same problems, they can find it through Searching :)
     
    Last edited: Oct 12, 2016
  30. Aetrix

    Aetrix

    Joined:
    Nov 23, 2013
    Posts:
    12
    Hello, i have a simple question i hope, im using your script for touch input. Can you please tell me how to implement "run". By "run" i would like if user swiped and didnt release the finger that player repeat movement in direction where player is moving. Im new with touch inputs and would like to hear from you what is the best practice for that. thank you, Andrej.
     
  31. rickbrunstedt

    rickbrunstedt

    Joined:
    Oct 17, 2016
    Posts:
    1
    NullReferenceException: Object reference not set to an instance of an object
    Player.Start () (at Assets/Scripts/Player.cs:17)

    # GameManager script
    Code (CSharp):
    1.  
    2. public static GameManager instance = null;
    3.  
    4. void Start () {
    5.         if (instance == null)
    6.             instance = this;
    7.         else if (instance != this)
    8.             Destroy(gameObject);
    9.  
    # Player script
    Code (CSharp):
    1.  
    2. protected override void Start () {
    3.         animator = GetComponent <Animator> ();
    4.         food = GameManager.instance.playerFoodPoints;    <------------ This is the line that breaks
    5.         base.Start();
    6.     }
    7.  
    For som reason it cant seem to find GameManager.instance in player script.
    In Enemy script it workes fine, I tested to print GameManager.instance.playerFoodPoints in enemy.

    The code that I'm showing from the game manager file, isn't that what I need to make it into a singelton?

    Thanks!
     
  32. ilumiMATI

    ilumiMATI

    Joined:
    Oct 19, 2016
    Posts:
    1
    Hi!

    I'm stuck at making GameController. After the game start this is always happening. I don't know what change to make it work correctly. :( help!

    upload_2016-10-20_17-17-40.png

    Effect in game:
    Bez tytułu.png

    BoardManager:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Random = UnityEngine.Random;
    5.  
    6. public class BoardManager : MonoBehaviour
    7. {
    8.     [Serializable]
    9.     public class Count
    10.     {
    11.         public int minimum;
    12.         public int maximum;
    13.  
    14.         public Count (int min, int max)
    15.         {
    16.             minimum = min;
    17.             maximum = max;
    18.         }
    19.     }
    20.  
    21.     public int columns = 8;
    22.     public int rows = 8;
    23.  
    24.     public Count wallCount = new Count(5, 9);
    25.     public Count foodCount = new Count(1, 5);
    26.     public GameObject exit;
    27.     public GameObject[] floorTiles;
    28.     public GameObject[] wallTiles;
    29.     public GameObject[] foodTiles;
    30.     public GameObject[] enemyTiles;
    31.     public GameObject[] OuterWallTiles;
    32.  
    33.     private Transform boardHolder;
    34.     private List <Vector2> gridPositions = new List<Vector2>();
    35.  
    36.     void InitialiseList()
    37.     {
    38.         gridPositions.Clear();
    39.  
    40.         for(int x = 1; x < columns - 1; x++)
    41.         {
    42.             for(int y = 1; x < rows - 1; y++)
    43.             {
    44.                 gridPositions.Add(new Vector2(x, y));
    45.             }
    46.         }
    47.     }
    48.  
    49.     void BoardSetup()
    50.     {
    51.         boardHolder = new GameObject("Board").transform;
    52.  
    53.         for (int x = -1; x < columns + 1;x++)
    54.         {
    55.             for(int y = -1; y < rows + 1;y++)
    56.             {
    57.                 GameObject toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)];
    58.                 if (x == -1 || x == columns || y == -1 || y == rows)
    59.                 {
    60.                     toInstantiate = OuterWallTiles[Random.Range(0, OuterWallTiles.Length)];
    61.                 }
    62.                 GameObject instance = Instantiate(toInstantiate, new Vector2(x, y), Quaternion.identity) as GameObject;
    63.  
    64.                 instance.transform.SetParent(boardHolder);
    65.             }
    66.         }
    67.     }
    68.  
    69.     Vector2 RandomPosition()
    70.     {
    71.         int randomIndex = Random.Range(0, gridPositions.Count);
    72.         Vector2 randomPosition = gridPositions[randomIndex];
    73.         gridPositions.RemoveAt(randomIndex);
    74.         return randomPosition;
    75.     }
    76.  
    77.     void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum)
    78.     {
    79.         int objectCount = Random.Range(minimum, maximum + 1);
    80.  
    81.         for(int i = 0; i< objectCount; i++)
    82.         {
    83.             Vector2 randomPosition = RandomPosition();
    84.             GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];
    85.             Instantiate(tileChoice, randomPosition, Quaternion.identity);
    86.         }
    87.     }
    88.  
    89.     public void SetupScene(int level)
    90.     {
    91.         BoardSetup();
    92.         InitialiseList();
    93.         LayoutObjectAtRandom(wallTiles, wallCount.minimum, wallCount.maximum);
    94.         LayoutObjectAtRandom(foodTiles, foodCount.minimum, foodCount.maximum);
    95.         int enemyCount = (int)Mathf.Log(level, 2f);
    96.         LayoutObjectAtRandom(enemyTiles, enemyCount, enemyCount);
    97.         Instantiate(exit, new Vector2(columns - 1, rows - 1), Quaternion.identity);
    98.     }
    99. }
    100.  
    GameManager:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public BoardManager boardScript;
    7.  
    8.     private int level = 3;
    9.  
    10.     void Awake()
    11.     {
    12.         GetComponent<BoardManager>();
    13.         InitGame();
    14.     }
    15.  
    16.     void InitGame()
    17.     {
    18.         boardScript.SetupScene(level);
    19.     }
    20.  
    21. }
    22.  
     
  33. amirkhabbaz

    amirkhabbaz

    Joined:
    Nov 5, 2016
    Posts:
    2
    HI ,i am watching the 5th tutorial now. till yet every thing was fine and before saving the GameManger object as a prefab and removing that from hierarchy and calling it with Loader, everything works just fine . but after doing that, there is not any view of the game both on scene and game tabs. i checked again and add the GameManager prefab Again as an object to the hierarchy and everything again was OK. i checked again and again and i did anything just like the tutorial. after adding the public static instance and ... to the GameManger.cs . i did create the Loader.cs and do as tutorial said then i added the loader script to the main camera and after that i dragged in the game manager prefab to the loader. But after playing the Game THERE IS NOTHING! . with the GameManager in the hierarchy , evereything is Just Fine and OK.


    my GameManger Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public static GameManager instance = null;
    7.     public BoardManager boardScript;
    8.  
    9.     private int level = 3;
    10.  
    11.     void Awake()
    12.     {
    13.         if (instance == null)
    14.             instance = this;
    15.         else if (instance != this)
    16.             Destroy(gameObject);
    17.  
    18.         DontDestroyOnLoad(gameObject);
    19.         boardScript = GetComponent<BoardManager>();
    20.         InitGame();
    21.     }
    22.  
    23.     void InitGame()
    24.     {
    25.         boardScript.SetupScene(level);
    26.     }
    27. }
    28.  

    My loader Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Loader : MonoBehaviour {
    5.  
    6.     public GameObject gameManager;
    7.  
    8.     void Awake () {
    9.         if (GameManager.instance == null)
    10.             Instantiate(gameManager);
    11.     }
    12.  
    13. }
    14.  
    Edited: SOLVED! my mistake, seems that i forgot to use == instead of = in the loader
    :
     
    Last edited: Nov 10, 2016
  34. bardan

    bardan

    Joined:
    Sep 12, 2016
    Posts:
    6
    man!! did u solve your problem , i am facing the same problem !!
     
  35. bardan

    bardan

    Joined:
    Sep 12, 2016
    Posts:
    6
    @broodj3 : jst go to playerscript on player prefab and set the blockinglayer to BlockingLayer;(for me the problem was that; hope you already guessed the problem and solved it)
     
  36. amirkhabbaz

    amirkhabbaz

    Joined:
    Nov 5, 2016
    Posts:
    2


    hi, i had the same problem but fortunately i solved that. first of all check your scripts in case of that there is some typo mistakes in them. i had some and after correcting them i got no error or even warning!(except one warning about hit variable not used, which is OK because we will use that at tutorial 13). BUT with using Unity 5.4 pro the main problem still was not solved at the time , i mean the player could only move for one time. so i used very debug and logs throw the scripts and i found the problem at the MovingObjects.cs at the co routine of SmoothMoving! it seems that the code in tutorial is not compatible with unity 5.4 or ... however i modified the code as follows and everything worked perfectly:

    Code (CSharp):
    1.     protected IEnumerator SmoothMovement (Vector2 end)
    2.     {
    3.         Vector2 n = new Vector2(transform.position.x - end.x, transform.position.y - end.y);
    4.         float sqrRemainingDistance = (n).sqrMagnitude;
    5.         while(sqrRemainingDistance > float.Epsilon)
    6.         {
    7.             Vector3 newPosition = Vector3.MoveTowards(rb2d.position, end, inverseMoveTime * Time.deltaTime);
    8.             rb2d.MovePosition(newPosition);
    9.             Vector2 n2 = new Vector2(transform.position.x - end.x, transform.position.y - end.y);
    10.             sqrRemainingDistance = (n2).sqrMagnitude;
    11.            /* Debug.Log(transform.position.x);
    12.             Debug.Log(transform.position.y);*/
    13.             yield return null;
    14.         }
    15.     }
    please notice the using of the Vector2 instead of Vector3 as argument of the method.
    may other friends whom had the same problem can solve this via my comment.
     
  37. JHTorkveen

    JHTorkveen

    Joined:
    Nov 2, 2016
    Posts:
    3
    Firstly, great tutorial! I'm very new to Unity still, so I got a little confused by all the scripts and what references what and so on towards the end, but I'm studying the commented scripts so I'll hopefully make sense of it soon.

    Either way, what I really want to ask, and I have been Googling this for hours on end to no avail, as I don't really know what exactly to look for and how it fits into all the other code, but would it be a huge operation to change the touch movement from swipe-based to single-touch based? I feel it would play much better if I just touched in either an horizontal or vertical position relative to the player and then moved a tile in that direction.

    My code is exactly like the tutorial code and everything works fine, basically looking at making my own version now. I am not looking for a complete blueprint, more a push in the right direction.

    TL;DR: How would I go about changing the touch system from swipe-based to touch-based?
     
  38. CruzEmily11

    CruzEmily11

    Joined:
    Nov 19, 2016
    Posts:
    1
    Hi! I really need some help. It seems like almost everything is working. But I'm having a great problem trying to make my player move. I've checked all the scripts and followed every single step of the tutorial, but I can't find the problem. I'm working in Winodws 10, Unity 5.4.1
    Also, when i hit the play button, my "day" sign and the "food" count doesn't appear, even though in Scene Mode I see it modifies when I press the arrow keys (Still, my player won't move and the text is not visible in Play Mode)
     
  39. Al1one

    Al1one

    Joined:
    Nov 21, 2016
    Posts:
    7
    Hi, so can someone explain how to create a "Restart" button, after u out of food, to restart game ?
    Also i interest if it possible to create menu with " start new game or continue game ", waiting for answer.
     
    Last edited: Nov 21, 2016
  40. Al1one

    Al1one

    Joined:
    Nov 21, 2016
    Posts:
    7
  41. Al1one

    Al1one

    Joined:
    Nov 21, 2016
    Posts:
    7
  42. Al1one

    Al1one

    Joined:
    Nov 21, 2016
    Posts:
    7
    up
     
  43. jourdanr

    jourdanr

    Joined:
    Sep 13, 2016
    Posts:
    4
    Hi everyone,

    I'm having an issue with how the game manager seems to be instantiating two game boards at once; when I hit play, there are two boards in the hierarchy and I start at Day 2. This has multiple weird effects:

    1) Enemies will be spawned on top of food or wall tiles because they're on different game boards
    2) When I reach the exit, the game manager instantiates another two boards
    3) The days go up in twos (it already starts at day two for whatever reason)
    4) When I attempt to move in the next level, I die because the food points counter, no matter what it was before, gets set to 0 and I starve.

    I've been following along with minimal difficulty - the mechanics are working, animations are fine, the UI is good (it is a really good tutorial!) but this issue I can't seem to fix. I've checked my board manager script, my game manager script AND my loader but nothing seems to be wrong. I'll leave them here so anyone who sees something I can't.

    GameManager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class GameManager : MonoBehaviour {
    8.  
    9.     public float levelStartDelay = 2f;
    10.     public static GameManager instance = null;
    11.     public BoardManager boardScript;
    12.     public float turnDelay = 0.1f;
    13.  
    14.     private Text levelText;
    15.     private GameObject levelImage;
    16.     private bool doingSetup;
    17.     private int level = 1;
    18.     private List<Enemy> enemies;
    19.     private bool enemiesMoving;
    20.  
    21.     public int playerFoodPoints = 100;
    22.     [HideInInspector] public bool playersTurn = true;
    23.  
    24.  
    25.     void Awake ()
    26.     {
    27.         if (instance == null)
    28.             instance = this;
    29.         else if (instance != null)
    30.             Destroy (gameObject);
    31.  
    32.         DontDestroyOnLoad (gameObject);
    33.         enemies = new List<Enemy>();
    34.         boardScript = GetComponent<BoardManager> ();
    35.         InitGame ();
    36.     }
    37.  
    38.     void OnLevelFinishedLoading (Scene scene, LoadSceneMode mode)
    39.     {
    40.         level++;
    41.         InitGame ();
    42.     }
    43.  
    44.     void OnEnable ()
    45.     {
    46.         SceneManager.sceneLoaded += OnLevelFinishedLoading;
    47.     }
    48.  
    49.     void OnDisable ()
    50.     {
    51.         SceneManager.sceneLoaded -= OnLevelFinishedLoading;
    52.     }
    53.  
    54.     void InitGame()
    55.     {
    56.         doingSetup = true;
    57.         levelImage = GameObject.Find ("LevelImage");
    58.         levelText = GameObject.Find ("LevelText").GetComponent <Text>();
    59.         levelText.text = "Day " + level;
    60.         levelImage.SetActive (true);
    61.         Invoke ("HideLevelImage", levelStartDelay);
    62.  
    63.         enemies.Clear();
    64.         boardScript.SetupScene (level);
    65.     }
    66.  
    67.     private void HideLevelImage ()
    68.     {
    69.         levelImage.SetActive (false);
    70.         doingSetup = false;
    71.     }
    72.  
    73.     void Update ()
    74.     {
    75.         if (playersTurn || enemiesMoving || doingSetup)
    76.             return;
    77.  
    78.         StartCoroutine (MoveEnemies ());
    79.     }
    80.  
    81.     public void AddEnemyToList (Enemy script)
    82.     {
    83.         enemies.Add (script);
    84.     }
    85.  
    86.     IEnumerator MoveEnemies ()
    87.     {
    88.         enemiesMoving = true;
    89.         yield return new WaitForSeconds (turnDelay);
    90.         if (enemies.Count == 0)
    91.         {
    92.             yield return new WaitForSeconds (turnDelay);
    93.         }
    94.         for (int i = 0; i < enemies.Count; i++)
    95.         {
    96.             enemies [i].MoveEnemy ();
    97.             yield return new WaitForSeconds (enemies [i].moveTime);
    98.         }
    99.         playersTurn = true;
    100.         enemiesMoving = false;
    101.     }
    102.  
    103.     public void GameOver ()
    104.     {
    105.         levelText.text = "After " + level + " days, you starved.";
    106.         levelImage.SetActive (true);
    107.         enabled = false;
    108.     }
    109. }
    110.  

    BoardManager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections.Generic;
    4. using Random = UnityEngine.Random;
    5.  
    6. public class BoardManager : MonoBehaviour {
    7.  
    8.     public class Count
    9.     {
    10.         public int minimum;
    11.         public int maximum;
    12.  
    13.         public Count (int min, int max)
    14.         {
    15.             minimum = min;
    16.             maximum = max;
    17.         }
    18.     }
    19.  
    20.     public int columns = 8;
    21.     public int rows = 8;
    22.  
    23.     public Count wallCount = new Count (5, 9);
    24.     public Count foodCount = new Count (1, 5);
    25.  
    26.     public GameObject exit;
    27.     public GameObject[] floorTiles;
    28.     public GameObject[] foodTiles;
    29.     public GameObject[] wallTiles;
    30.     public GameObject[] enemyTiles;
    31.     public GameObject[] outerwallTiles;
    32.  
    33.     private Transform BoardHolder;
    34.     private List <Vector3> gridPositions = new List <Vector3> ();
    35.  
    36.     void InitialiseList()
    37.     {
    38.         gridPositions.Clear ();
    39.  
    40.         for (int x = 1; x < columns - 1; x++)
    41.         {
    42.             for(int y = 1; y < rows - 1; y++)
    43.             {
    44.                 gridPositions.Add (new Vector3 (x, y, 0f));
    45.             }
    46.         }
    47.     }
    48.  
    49.     void BoardSetup()
    50.     {
    51.         BoardHolder = new GameObject ("Board").transform;
    52.  
    53.         for(int x = -1; x < columns + 1; x++)
    54.                         {
    55.             for(int y = -1; y < rows + 1; y++)
    56.                             {
    57.                                 GameObject toInstantiate = floorTiles[Random.Range (0, floorTiles.Length)];
    58.  
    59.                                 if (x == -1 || x == columns || y == -1 || y == rows)
    60.                                     toInstantiate = outerwallTiles [Random.Range (0, outerwallTiles.Length)];
    61.  
    62.                                 GameObject instance = Instantiate (toInstantiate, new Vector3 (x, y, 0f), Quaternion.identity) as GameObject;
    63.  
    64.                                 instance.transform.SetParent (BoardHolder);
    65.                             }
    66.                         }
    67.     }
    68.  
    69.                     Vector3 RandomPosition ()
    70.     {
    71.         int randomIndex = Random.Range (0, gridPositions.Count);
    72.  
    73.         Vector3 randomPosition = gridPositions [randomIndex];
    74.  
    75.         gridPositions.RemoveAt (randomIndex);
    76.  
    77.         return randomPosition;
    78.     }
    79.         void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
    80.     {
    81.         int objectCount = Random.Range (minimum, maximum + 1);
    82.                          
    83.         for (int i = 0; i < objectCount; i ++)
    84.                         {
    85.                             Vector3 randomPosition = RandomPosition ();
    86.  
    87.                             GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length) ];
    88.  
    89.                             Instantiate (tileChoice, randomPosition, Quaternion.identity);
    90.                         }
    91.     }
    92.                     public void SetupScene (int level)
    93.     {
    94.                         BoardSetup ();
    95.                         InitialiseList ();
    96.         LayoutObjectAtRandom (wallTiles, wallCount.minimum, wallCount.maximum);
    97.                         LayoutObjectAtRandom (foodTiles, foodCount.minimum, foodCount.maximum);
    98.         int enemyCount = (int)Mathf.Log (level, 2f);
    99.                         LayoutObjectAtRandom (enemyTiles, enemyCount, enemyCount);
    100.                         Instantiate (exit, new Vector3(columns - 1, rows - 1, 0f), Quaternion.identity);
    101.     }
    102. }
    103.  
    Loader:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Loader : MonoBehaviour {
    5.  
    6.     public GameObject gameManager;
    7.  
    8.     void Awake ()
    9.     {
    10.         if (GameManager.instance == null)
    11.             Instantiate (gameManager);  
    12.     }
    13.  
    14.  
    15. }
    16.  
    EDIT: Okay, so turns out that having InitGame() called twice in GameManager was causing all kinds of ridiculousness. I got rid of the one in Awake and now things are much better. However, I'm still starting at Day 2 :confused:
     
    Last edited: Nov 24, 2016
  44. Al1one

    Al1one

    Joined:
    Nov 21, 2016
    Posts:
    7
    up
     
  45. Deleted User

    Deleted User

    Guest

    Instead of "upping" did you search the other tutorials? At least one of them teaches how to make a "restart" button.
     
  46. Al1one

    Al1one

    Joined:
    Nov 21, 2016
    Posts:
    7
    yup, but in the level system like in this game, all methods that i found, doesn't work, that's why i'm upping this topic
     
  47. dan_stewart

    dan_stewart

    Joined:
    Nov 28, 2016
    Posts:
    1
    I cant download the assets from the store for this project an apache license terms box appears and there is no accept button to close it
     
    Last edited: Nov 29, 2016
  48. CamoClayton

    CamoClayton

    Joined:
    Nov 29, 2016
    Posts:
    5
    Firstly thank you for the tutorial. It is easy enough to follow even for a novice like me.
    I am having trouble with this script attached.
    I have also attached a screen shot of the error messages in Unity.
    I am stumped so any help would be greatly appreciated.
     

    Attached Files:

  49. Al1one

    Al1one

    Joined:
    Nov 21, 2016
    Posts:
    7
    so any ideas ?
     
  50. 8_Stackz

    8_Stackz

    Joined:
    Oct 26, 2015
    Posts:
    1
    -----------------------------------------------------------
    Hey there

    I too had this problem, but I solved it using the following :)

    GameManager:
    Code (csharp):
    1.  
    2.     void Awake () {
    3.         if (instance == null) {
    4.             instance = this;
    5.         } else if (instance != this) {
    6.             Destroy (gameObject);
    7.         }
    8.  
    9.          //For persistance between scenes
    10.          DontDestroyOnLoad (gameObject);
    11.  
    12.          enemies = newList <Enemy> ();
    13.          boardScript = GetComponent <BoardManager> ();
    14.          InitGame ();
    15.     }
    16.  
    17.      private void OnLevelFinishedLoading (Scene scene, LoadSceneMode mode) {
    18.          if (level > 1) {
    19.              InitGame ();
    20.          }
    21.          level++;
    22.      }
    23.  
    24.      private void OnEnable () {
    25.          SceneManager.sceneLoaded += OnLevelFinishedLoading;
    26.      }
    27.  
    28.      privatevoidOnDisable(){
    29.          SceneManager.sceneLoaded-=OnLevelFinishedLoading;
    30.      }
    31.  
    Just add to the level count after the initGame call in OnLevelFinishedLoading, and only call initGame after the first time it has been loaded.
    Hope this helps :)
     
    Deleted User and Matthew-Schell like this.
Thread Status:
Not open for further replies.