Is it possible to have one script to call upon levels please help!!
Hey everyone I was wondering if anyone could help me or point me in the right direction. I have a script for my game for loading a specific scene when clicking on the correct object. When I click on the right object, I have it go to a win screen. When I click on the wrong object, I have it go to a lose screen. Each of those scenes have a button for next level or reset level. I have a script for loading the next level as shown:
#pragma strict
function Start_Game_Button ()
{
Application.LoadLevel("Level 1");
}
My question is do I have to make that script for every scene to load the next level/scene (ex 1 2 3 4 5 etc)? Or is there a way to make a script that will keep going to the next level without me having to write the script:
{
Application.LoadLevel("Level 1");
{
Application.LoadLevel("Level 2");
{
Application.LoadLevel("Level 3");
{
Application.LoadLevel("Level 4");
}
if anyone can help me or point me in the right direction I would greatly appreciate it. Thank you.
Answer by Ygedey · Mar 28, 2016 at 10:25 PM
You could make a function in your script which moves to the next level by using the index that the scene has in your build settings.
If you read this page SceneManager you can see that SceneManager.LoadScene (Note that SceneManager.LoadScene has replaced Application.LoadLevel) can pick a scene from the index that it has in the build settings.
This means that if you keep track of the index of the scene you are in, you could simply do:
function LoadNextLevel(){
currentIndex++;
SceneManager.LoadScene(currentIndex);
}
Edit: As Toon_Werawat mentioned in another similar question, you should be able to use
SceneManager.LoadScene(SceneManager.GetActiceScene().buildIndex + 1);
Which would save you from keeping track of the current index in a static variable.
Answer by Jalah_LLC · Mar 31, 2016 at 07:17 PM
Ok thanks for your help! I did try what you suggested as Toon_Werawat stated, but since my index includes a "Lose Scene" and "Win Scene", it kinda messes up the buildindex counting up. I found this:
The only way to accomplish this that I have found is to make multiple game over scenes even if they look the same.
Then position them in the build index underneath each level. So "level 1" is above "Level1GameOverScene" and "Level 2" is above "Level2GameOverScene" and so on.
Then add the script that says SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex -1); onto The "Try again" text for example in your GameOver scene.
And attach another script onto the thing that causes the game to be over that says - SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);
This way the scenes are relying working with each other as long as they are positioned correctly in the build index. This is the only way I can think of that is easy but I can almost guarantee that someone experienced can find a much easier way using code I've never even heard of.
I hope this helps others in my situation but if not, good luck finding a solution to your problem. :)
however, I feel that it is not efficient, and I am sure there is a better way. Is it possible to have a win and lose screen load up within a level scene? That way it is not considered a "scene". Like how in flappy bird when you die, a popup comes up saying game over, despite still saying in the same scene. Does anyone know? Any help I would greatly appreciate it.
What if you put your "Game over" and "Win Scene" before your actual levels in your build settings? Then, when you want to start playing through the levels, load the first level with its name, and then let them roll on. If the player loses, use the name of the game over screen to head to that scene.
I can try that! When you say load the first level with its name, then let them roll on, what do I do to accomplish that? Since the "Win Scene" has a next level button it has this:
import UnityEngine.Scene$$anonymous$$anagement;
#pragma strict
function Start_Game_Button ()
{
Scene$$anonymous$$anager.LoadScene ("Level 2");
so what script can I do to let the rest roll on from the build index without contradicting this script?
Your answer
Follow this Question
Related Questions
Scene is loading in Game Mode, but not in the build. Why? 2 Answers
level manger and starting scene will not work together 0 Answers
How can i get the value of the Scrpit what is attached on the Image 0 Answers
Creating a very Long Level Select Scene 0 Answers
How to GetComponent for SteamVR_TrackedObj when script not attached to controller 0 Answers