Disable load new level until all enemies are dead
I am making a simple top down 2d dungeon crawler RPG and I want it to be set so that the player cant load into a new scene until all enemies are dead. I have it set up with an empty object with a box collider 2d and when the player collides with that it loads into the scene i have set it to load to. But i dont want to the player to be able to load into the new scene till they've killed all the enemies in that scene. Im thinking it might be something likeif(other.gameObject.tag == "Enemy") { //then somehow disable load new level }
but i dont know much about scripting so im probably WAY off lol. Thanks for any help :)
Answer by Cuttlas-U · May 04, 2017 at 04:48 AM
Hi; u can simply create an int variable and by killing each enemy increase its value by 1 ; then with a simple if statement check if the variable is for example equal to 10 then Active the object that has the collider on it witch player can go to next level with it;
well each enemy has its own script and u cant create this function in enemy script cause that script will be destroy when the enemy die ;
so u need to create another script in your scene attache it to a Empty GameObject and name it for example "Manager" then create a function in it witch do the things like this :
public int NeedEnemyDiedToGoNextScen;
public int CurrentEnemyDied;
public GameObject NextLevelLoaderObject;
public void CheckPlayerCanGoNextLevel()
{
CurrentEnemyDied += 1;
if (CurrentEnemyDied >= NeedEnemyDiedToGoNextScen)
{
NextLevelLoaderObject.SetActive(true);
}
}
so each time this function "CheckPlayerCanGoNextLevel" is called it will increase the value by 1 and then check if its enough to active the object; So now the only thing u need is calling this function witch should happen when an enemy die so u simply call it in your enemy script or where ever u kill an enemy u call this way :
GameObject.FindObjectOfType<Manager>().CheckPlayerCanGoNextLevel();
Hope u solve it ;
Answer by Cookieg82 · May 18, 2017 at 09:09 PM
I implemented this code and it's good. I can lend another example of my code if you like?
Your answer
Follow this Question
Related Questions
Having a couple of problems regarding 2D Movement. 1 Answer
I'm working with the corgi-engine/inventory engine and need help with a script. 1 Answer
How do I save data on a webGL version of my game? 0 Answers
Enemy death animation keeps looping (Unity 2D) 1 Answer
Unity jump from one canvas scene to another scene with canvas 2 Answers