How to switch to next level after completeing the task in current level
Hi dudes, I am designing a game with 3 Levels for example if we have 10 enemies in our first level and once its completed we have to switch to the next level and so on. Here my doubt is in coding part once i complete my task by destroying my enemies in Level 1 how can jump to next level in this also i can load my next level by using "Application.LoadLevel("name of scene")" but the thing is how to identify that my enemies are destroyed and to loaded the next level automatically Please help me
guys I'm making a breakout game and I don't know how to like get it to go straight to the next lvl after I have finish the one I'm on.( by any chance could I get a step by step solution with scripts plz).
Answer by robertmathew · Apr 01, 2011 at 07:41 AM
// Loads the level with index 0
Application.LoadLevel (0);
// Load the level named "HighScore".
Application.LoadLevel ("HighScore");
This function loads level by its index. You can see the indices of all levels using the File->Build Settings... menu in Unity. Before you can load a level you have to add it to the list of levels used in the game. Use File->Build Settings... in Unity and add the levels you need to the level list there.
Thank you Buddy ! $$anonymous$$y doubt is that how to detect that my enemies are destroyed so that once its detected as my enemies in level 1 are completely destroyed it has to load next level
Answer by sriram90 · Apr 01, 2011 at 07:33 AM
hey,
Do you have all those levels in different scenes. if you have it as different scenes means first add all those scenes when you build the project. when you hit build it shows a button name as ADD CURRENT.first you just hit it and add all those levels of your project.after you add this it'll appear like this,
Scene1 Name - 0 Scene2 Name - 1 Scene3 Name - 2
you'll get like this,if you want to move 2nd scene after killing those enemies means just use Application.LoadLevel(1), and move 3rd scene means use Application.LoadLevel(2).
see this link http://unity3d.com/support/documentation/ScriptReference/Application.LoadLevel.html
if you're not using different scene you cant use Application.LoadLevel.
Simple and enjoy.
Thank you Buddy ! $$anonymous$$y doubt is that how to detect that my enemies are destroyed so that once its detected it has to load next level
just use counts....consider you're having 10 enemies when game get start...decrease that count when enemies destroy...finally if you got count 0 means move to next level...thats it...easy to make the code...
Answer by e-bonneville · Apr 01, 2011 at 11:05 AM
Well, in order to detect when all the enemies are destroyed (in order to load the next level), you'll need to keep some kind of counter to keep track of how many enemies are left. That way, when the counter reaches zero, you know you have no enemies left, and you can advance to the next level.
I recently gave an answer to a very similar question here. Since the script is almost exactly what you need, just replace "cannon" with "enemy" and you should be good to go. Oh, and you'll also need a way to keep track of what level you're on, so you that you can advance to the next one.
Thank you Elliot Let me try with this and if i have any doubt i will get back to you .
Answer by patrick · Aug 31, 2013 at 01:23 PM
function Start() { application.LoadLevel("levelName");// switches to this level name on start // OR you can use this Application.LoadLevel (0);// which loads the scein in the index order }
Answer by Calum1015 · May 02, 2015 at 06:11 AM
This is a very old question, but I will answer anyway's to help anyone struggling to understand as of yet. Here are a list of steps.
Add a few variables to your script(in this case a Boolean, a Float and a second Float).
Use a Boolean value to detect if all the enemies are killed in a level.
Use an if statement to find out if all the enemies are killed using one of the float values.(this will probably make more sense in the code provided)
Load a level if all enemies are killed.
Repeat Steps 2-4 in level 2 and 3.
With that second float I told you to add your going to create a list of enemies killed.
Script is in C#.
using UnityEngine;
using System.Collections;
public class switchLevel : Monobehavior
{
public bool allEnemiesKilled;
public float enemiesInLevel = 10.0f;
public float enemiesKilled;
void Awake ()
{
DontDestroyOnLoad(this); //Ensures we don't lose our variables, playerPrefs work good for this too
}
void Update ()
{
if(enemiesKilled < enemiesInLevel ) //if enemies kill is greater than or equal to the amount of enemiesInLevel
{
allEnemiesKilled = true; //allEnemiesKilled is equal to true
}
if(allEnemiesKilled = true) //if allEnemiesKilled is equal to true, then load the level specified
{
Application.LoadLevel("yourSecondLevelHere");
}
}
}
Your answer
Follow this Question
Related Questions
Save and Load Audio from web 0 Answers
Fast Converting RenderTexture to Texture2D 1 Answer
Saving/loading inherited class scripts (C#) 1 Answer
loading player with item to have ? 1 Answer
VR Hourglass 0 Answers