How would I go about adding a “R” button press to restart the 2D Rouge Like Tutorial game once your character dies.
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?
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.
So if you get the GameManager Script from the tutorial I add the following code snips
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
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.
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:
if(Input.GetKeyDown(KeyCode.R))
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Just skip the part about game over/restart to test to be sure … narrowing down the issue
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public float levelStartDelay = 2f;
public float turnDelay = 0.1f;
public int playerFoodPoints = 100;
public static GameManager instance = null;
[HideInInspector] public bool playersTurn = true;
private Text levelText;
private Text restartText;
private GameObject levelImage;
private BoardManager boardScript;
private int level = 1;
private List<Enemy> enemies;
private bool enemiesMoving;
private bool doingSetup = true;
private bool gameOver;
private bool restart;
void Awake()
{
gameOver = false;
restart = false;
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
enemies = new List<Enemy>();
boardScript = GetComponent<BoardManager>();
InitGame();
}
void OnLevelWasLoaded(int index)
{
level++;
InitGame();
}
void InitGame()
{
doingSetup = true;
levelImage = GameObject.Find("LevelImage");
levelText = GameObject.Find("LevelText").GetComponent<Text>();
restartText = GameObject.Find ("RestartText").GetComponent<Text> ();
restartText.text = "";
levelText.text = "Day " + level;
levelImage.SetActive(true);
Invoke("HideLevelImage", levelStartDelay);
enemies.Clear();
boardScript.SetupScene(level);
}
void HideLevelImage()
{
levelImage.SetActive(false);
doingSetup = false;
}
void Update()
{
StartCoroutine (MoveEnemies ());
if (restart)
{
if (Input.GetKeyDown (KeyCode.R))
{
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
}
}
if(playersTurn || enemiesMoving || doingSetup)
return;
}
public void AddEnemyToList(Enemy script)
{
enemies.Add(script);
}
public void GameOver()
{
levelText.text = "After " + level + " days, you starved.";
levelImage.SetActive(true);
enabled = false;
gameOver = true;
}
IEnumerator MoveEnemies()
{
enemiesMoving = true;
yield return new WaitForSeconds(turnDelay);
if (enemies.Count == 0)
{
yield return new WaitForSeconds(turnDelay);
}
for (int i = 0; i < enemies.Count; i++)
{
enemies.MoveEnemy ();
yield return new WaitForSeconds(enemies.moveTime);
}
playersTurn = true;
enemiesMoving = false;
if (gameOver)
{
restart = true;
}
}
}
Please read this thread, and edit your post : Using code tags properly
Try what I suggested about just looking for the Input. Perhaps you want to check ‘restart’ before you 'return ’ from update, too. (“return early in the Update loop” **).
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…
also the score doesnt reset, the level starts with the previous food counter when pressed r for reset.
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.
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.