- Home /
Question by
Sam_Se7erne · May 29, 2015 at 05:42 AM ·
endgamedungeonend
2D rouguelike, ending the game.
I followed the rouguelike tutorial on the site but I am trying a add a code in the game manager that will end the game after successfully completing 10 levels, showing a 'You Escaped!' message and stop loading more levels. I can get the message to pop up but it disappears and loads the next level as before. I am very new to coding and have no idea what to try. Code below and I've highlighted the current ending game code, any suggestions please?
using UnityEngine;
using System.Collections;
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 GameObject levelImage;
private BoardManager boardScript;
private int level = 1;
private List<Enemy> enemies;
private bool enemiesMoving;
private bool doingSetup = true;
void Awake()
{
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> ();
levelText.text = "Day " + level;
levelImage.SetActive (true);
Invoke ("HideLevelImage", levelStartDelay);
enemies.Clear ();
boardScript.SetupScene (level);
if (level >= 2) {
levelText = GameObject.Find ("LevelText").GetComponent<Text> ();
levelText.text = "You Escaped!";
levelImage.SetActive (true);
}
}
void HideLevelImage()
{
levelImage.SetActive(false);
doingSetup = false;
}
void Update()
{
if(playersTurn || enemiesMoving || doingSetup)
return;
StartCoroutine (MoveEnemies ());
}
public void AddEnemyToList(Enemy script)
{
enemies.Add(script);
}
void EndGame()
{
if (level >= 2) {
levelText = GameObject.Find ("LevelText").GetComponent<Text> ();
levelText.text = "You Escaped!";
levelImage.SetActive (true);
}
}
public void GameOver()
{
levelText.text = "After " + level + " days, you starved.";
levelImage.SetActive (true);
enabled = false;
}
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[i].MoveEnemy ();
yield return new WaitForSeconds(enemies[i].moveTime);
}
playersTurn = true;
enemiesMoving = false;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613200347im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with a killbox 1 Answer
how to make scrolling credits? 1 Answer
When my car crashes into the guards, the game is supposed to be over! 0 Answers
how to check movement ended? 1 Answer
How do I end my level? 2 Answers