Here is my script where I believe the level incrementing is not working the way it’s supposed to:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public float levelStartDelay = 2f;
public float turnDelay = 0.1f;
public static GameManager instance = null;
public BoardManager boardScript;
public int playerFoodPoints = 100;
[HideInInspector] public bool playersTurn = true;
private Text levelText;
private GameObject levelImage;
private int level = 1; // Here is where level is declared
private List<Enemy> enemies;
private bool enemiesMoving;
private bool doingSetup = true;
void Awake()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
enemies = new List<Enemy>();
DontDestroyOnLoad(gameObject);
boardScript = GetComponent<BoardManager>();
InitGame();
}
void OnLevelFinishedLoading (Scene scene, LoadSceneMode mode)
{
level++; // Here is where I increment the level
InitGame();
}
void OnEnable()
{
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
void OnDisable()
{
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
public void GameOver()
{
if(level == 1)
{
levelText.text = "In just one day, you starved.";
levelImage.SetActive(true);
}
else
{
levelText.text = "After " + level + " days, you starved.";
levelImage.SetActive(true);
}
enabled = false;
}
void InitGame()
{
doingSetup = true;
levelImage = GameObject.Find("Level Image");
levelText = GameObject.Find("Level Text").GetComponent<Text>();
levelText.text = "Day " + level;
levelImage.SetActive(true);
Invoke("HideLevelImage", levelStartDelay);
enemies.Clear();
boardScript.SetupScene(level);
}
private void HideLevelImage()
{
levelImage.SetActive(false);
doingSetup = false;
}
void Update()
{
if (playersTurn || enemiesMoving || doingSetup)
return;
StartCoroutine(MoveEnemies());
}
public void AddEnemyToList(Enemy script)
{
enemies.Add(script);
}
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;
}
}