Load Level script problem
So i have MainMenu scene, EndLevel scene, and 8 Levels scenes (Names: Level01, Level02,Level03...). On MainMenu and EndLevel i have button and added the function StartGameNew this is my code:
#pragma strict
var levelNumber = 1;
function StartGameNew()
{
Application.LoadLevel("Level0" + levelNumber);
if(levelNumber <= 8)
levelNumber++;
else levelNumber = 1;
}
The problem is here when i click on the button play again this error pops up: Scene 'Level00' (-1) couldn't be loaded because it has not been added to the build settings or the asset bundle has not been loaded. To add a scene to the build settings use the menu File->Build Settings...
Why it wants to load Scene 'Level00' ? not Level02? how to fix this where is the problem in my code?
Answer by Statement · Nov 07, 2015 at 10:57 PM
Scene 'Level00'
var levelNumber = 1;
// ...
Application.LoadLevel("Level0" + levelNumber);
levelNumber must then be zero. If you for example originally had levelNumber as 0, or changed levelNumber in the inspector to 0, it will retain that information.
Why it wants to load Scene 'Level00' ? not Level02?
Well, first I don't understand why you expect it to load Level02. I could agree that you could expect it to load Level01 because levelNumber is 1 in the script. However levelNumber is a public, serialized variable and if you check the number on the script on the inspector, you'll find out it's 0. Why? Because one power of Unity is to be able drive data from the editor.
Simple fix. Change it to 1 or 2 or whatever you want it to be in the inspector. If you don't want that variable to be editable in the inspector, you can make it private.
private var levelNumber = 1;
http://docs.unity3d.com/$$anonymous$$anual/Inspector.html
Any property that is displayed in the Inspector can be directly modified. Even script variables can be changed without modifying the script itself. You can use the Inspector to change variables at runtime to experiment and find the magic gameplay for your game. In a script, if you define a public variable of an object type (like GameObject or Transform), you can drag and drop a GameObject or Prefab into the Inspector to make the assignment.
Tnx for helping but i already solved this by my self like this
$$anonymous$$ain$$anonymous$$enu.js Code:
static var levelNumber = 1;
function StartGameNew()
{
Application.LoadLevel(levelNumber);
}
Game$$anonymous$$aster.js code:
function LoadNextLevel ()
{
finalScore = currentScore;
$$anonymous$$ain$$anonymous$$enu.levelNumber++;
if($$anonymous$$ain$$anonymous$$enu.levelNumber == 9)
{
$$anonymous$$ain$$anonymous$$enu.levelNumber = 1;
}
Application.LoadLevel ("EndLevel");
}
so its like this when player loses calls LoadNextLevel() function and goes to EndlLevel(scene) and in that scene i have button with function StartGameNew() from $$anonymous$$ain$$anonymous$$enu.js
Answer by feliperapussi · Nov 07, 2015 at 07:05 PM
this error is because you do not put the scenes in the build settings you have to go to File> Build Settings , and grovellest all his scenes in it and position correctly in the first place the main menu, then the scene 1,2,3 ... and finally the End
try it , you'll probably show up another error then why not make sense to me that this wanting to do this code
i know that and i have added them but smth other is the problem, btw my level's names are like htis Level01 Level02 so i want to go open them in order so i think this is good Application.LoadLevel("Level0" + levelNumber); and its working but somehow ins$$anonymous$$d Level01-8 its loading first Level01 then tries Level00 and i dont have Level00 and i dont know how it gets to Level00 my code is made to go from 1 to 8 not 0..