Hi guys. I got a quick question for you. I have already everything prepared. I am making a simple launching game, where you need to kill all enemies to pass to the next level. I have 2 methods GoToNextLevel()
and AllMonsterDied(); . Need to make something like with if statement like if
(! allMonestersDied)
{
restart level
}
public class LevelController : MonoBehaviour
{
[SerializeField] string _nextLevelName;
Monster[] _monsters;
void OnEnable()
{
_monsters = FindObjectsOfType<Monster>();
}
void Update()
{
int currentLevel = SceneManager.GetActiveScene().buildIndex;
if (currentLevel >= PlayerPrefs.GetInt("levelsUnlocked"))
{
PlayerPrefs.SetInt("levelsUnlocked", currentLevel + 1);
}
if (MonsterAreAllDead() )
{
GoToNextLevel();
}
Debug.Log("Level" + PlayerPrefs.GetInt("levelsUnlocked") + "UNLOCKED");
}
void GoToNextLevel()
{
Debug.Log("Go to next level" + _nextLevelName);
SceneManager.LoadScene(_nextLevelName);
}
bool MonsterAreAllDead()
{
foreach (var monster in _monsters)
{
if (monster.gameObject.activeSelf)
return false;
}
return true;
}
}
but need that happens after 3 times fail to kill all monsters and some icons in-game which is connected and show number of attempts left.
If needed I will post the whole code of methods.
Thanks a lot. <3