Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Restarting 2D Rouge Like Game Tutorial with "R" keyboard press

Discussion in 'Scripting' started by Beta4, Feb 16, 2018.

  1. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    How would I go about adding a "R" button press to restart the 2D Rouge Like Tutorial game once your character dies.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,909
    In an Update() function somewhere, check if the game is over, and when the game is over, check if the R key gets pressed, then (probably) just reload the current scene.

    What have you tried so far?
     
  3. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    you could simply reload the scene, although without mentioning if your rouge-like has map layout changes, or if the character keeps certain equipment or stats upon respawn, its hard to say if merely reloading the scene is a good solution.
     
  4. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    So if you get the GameManager Script from the tutorial I add the following code snips

    https://unity3d.com/learn/tutorials...al/adding-ui-level-transitions?playlist=17150


    So in the Game manager script I declare 2 variables before the Void Awake () function.

    private bool gameOver;
    private bool restart;

    Then In the Awake () function I add

    gameOver = false;
    restart = false;

    In the GameOver () function I add

    gameOver = true;

    Then in the Update () function I add
    if (restart)
    {
    if (Input.GetKeyDown (KeyCode.R))
    {
    SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
    }
    }

    Then in the IEnumerator MoveEnemies() I add

    if (gameOver)
    {
    restart = true;
    }

    But when I do it 95% of the time it doesn't work and when it does it restarts the last level the character dies with a negative health and doesn't do anything. Weird, it rarely restarts the level but when it does it fails as mentioned above
     
    Last edited: Feb 16, 2018
  5. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    Also I understand that by adding

    SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);

    Its actually just restarting the same level of demise.

    I want to know why it doesn't work 95% of the time. That's what I want to know and then deal with restarting at level 1.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Perhaps you should post a bit more of your code, in case the problem lies elsewhere.

    Oh, and you could also try something really simple like:
    Code (csharp):
    1. if(Input.GetKeyDown(KeyCode.R))
    2.    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    Just skip the part about game over/restart to test to be sure .. narrowing down the issue :)
     
  7. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.SceneManagement;
    5. using System.Collections.Generic;
    6. using UnityEngine.UI;              
    7.  
    8.    public class GameManager : MonoBehaviour
    9.    {
    10.        public float levelStartDelay = 2f;                  
    11.         public float turnDelay = 0.1f;                        
    12.         public int playerFoodPoints = 100;                    
    13.         public static GameManager instance = null;          
    14.         [HideInInspector] public bool playersTurn = true;    
    15.      
    16.      
    17.         private Text levelText;
    18.         private Text restartText;                            
    19.         private GameObject levelImage;                  
    20.         private BoardManager boardScript;                  
    21.         private int level = 1;                              
    22.         private List<Enemy> enemies;                      
    23.         private bool enemiesMoving;                          
    24.         private bool doingSetup = true;                
    25.      
    26.       private bool gameOver;
    27.       private bool restart;
    28.  
    29.         void Awake()
    30.        {
    31.            gameOver = false;
    32.            restart = false;
    33.  
    34.  
    35.  
    36.             if (instance == null)
    37.              
    38.            
    39.                 instance = this;
    40.          
    41.        
    42.             else if (instance != this)
    43.              
    44.                 Destroy(gameObject);
    45.          
    46.  
    47.             DontDestroyOnLoad(gameObject);
    48.          
    49.  
    50.             enemies = new List<Enemy>();
    51.          
    52.  
    53.             boardScript = GetComponent<BoardManager>();
    54.          
    55.      
    56.             InitGame();
    57.        }
    58.      
    59.    
    60.         void OnLevelWasLoaded(int index)
    61.        {
    62.      
    63.             level++;
    64.      
    65.             InitGame();
    66.        }
    67.      
    68.      
    69.         void InitGame()
    70.        {
    71.        
    72.             doingSetup = true;
    73.          
    74.      
    75.             levelImage = GameObject.Find("LevelImage");
    76.          
    77.    
    78.             levelText = GameObject.Find("LevelText").GetComponent<Text>();
    79.          
    80.        
    81.             restartText = GameObject.Find ("RestartText").GetComponent<Text> ();
    82.  
    83.             restartText.text = "";
    84.  
    85.  
    86.             levelText.text = "Day " + level;
    87.          
    88.  
    89.             levelImage.SetActive(true);
    90.          
    91.  
    92.             Invoke("HideLevelImage", levelStartDelay);
    93.          
    94.    
    95.             enemies.Clear();
    96.          
    97.  
    98.             boardScript.SetupScene(level);
    99.          
    100.        }
    101.      
    102.      
    103.         void HideLevelImage()
    104.        {
    105.             levelImage.SetActive(false);
    106.          
    107.             doingSetup = false;
    108.        }
    109.      
    110.  
    111.         void Update()
    112.        {
    113.        
    114.             StartCoroutine (MoveEnemies ());
    115.  
    116.            if (restart)
    117.           {
    118.                   if (Input.GetKeyDown (KeyCode.R))
    119.              {
    120.                    SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
    121.              }
    122.           }
    123.  
    124.             if(playersTurn || enemiesMoving || doingSetup)    
    125.          
    126.                 return;  
    127.  
    128.        }
    129.      
    130.  
    131.         public void AddEnemyToList(Enemy script)
    132.        {
    133.        
    134.             enemies.Add(script);
    135.        }
    136.      
    137.      
    138.      
    139.         public void GameOver()
    140.        {
    141.        
    142.             levelText.text = "After " + level + " days, you starved.";
    143.          
    144.      
    145.             levelImage.SetActive(true);
    146.          
    147.      
    148.             enabled = false;
    149.  
    150.             gameOver = true;
    151.        }
    152.      
    153.  
    154.         IEnumerator MoveEnemies()
    155.        {
    156.        
    157.             enemiesMoving = true;
    158.          
    159.          
    160.             yield return new WaitForSeconds(turnDelay);
    161.          
    162.    
    163.             if (enemies.Count == 0)
    164.            {
    165.            
    166.                 yield return new WaitForSeconds(turnDelay);
    167.            }
    168.          
    169.  
    170.             for (int i = 0; i < enemies.Count; i++)
    171.            {
    172.        
    173.                 enemies.MoveEnemy ();
    174.              
    175.        
    176.                 yield return new WaitForSeconds(enemies.moveTime);
    177.            }
    178.      
    179.             playersTurn = true;
    180.          
    181.      
    182.             enemiesMoving = false;
    183.  
    184.               if (gameOver)
    185.                   {
    186.                       restart = true;
    187.                   }
    188.        }
    189.    }
    190.  
     
    Last edited: Feb 16, 2018
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  9. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    oh yes i reversed them in the code- still nothing-

    also i did remove the gameover and start calls and it does load up with day2 day3 etc. so what could it be?? I'confused...o_O
    also the score doesnt reset, the level starts with the previous food counter when pressed r for reset.
     
    Last edited: Feb 16, 2018
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not sure why it's not working with 'restart', but the food points remain the same because that script is preserved across loads. If you want the food to reset, you could do that in code when you restart the level, either on the key press or level loaded.

    You could try putting some Debug.Log messages in there, to confirm that variables are updating where you expect, etc, to try to track it down.
     
  11. Beta4

    Beta4

    Joined:
    Nov 6, 2014
    Posts:
    34
    I think it maybe has to do with the other scripts that something is not being turned off or something. I'm still new at this, hopefully I will figure it out. Maybe I need to turn something off in the other scripts for it to work properly or reset something. I appreciate the help.