How would I load a new scene when a score that is scored in a string is reached?
This is a code that I am working on for a space shooter. Part of it works and my score does increase by 7 every second but my script does not load my scene when the number reaches 1000. I have made sure that my scene is a part of my build settings and I have tried some fixes but it doesn't load even though I don't get any sort of error. Any Ideas?
#pragma strict
function Start(){
InvokeRepeating("EachSecond",1.0,1.0);
}
var score: int = 0;
var addscore = 7;
function EachSecond()
{
score=score+addscore;
}
function OnGUI(){
GUI.Box(Rect(1000,10,100,25), score.ToString());
}
function WinningScore()
{
if (score >= 1000 )
{
Application.LoadLevel("Win");
}
}
Answer by lienadnellum · Nov 09, 2015 at 06:01 PM
I am pretty sure that variable should be an integer that corresponds to the scene you want to load in build settings for loading levels make a new scene, add your stuff in it, then save scene call it "level1", make a new scene put your stuff into it and save the scene call it "level2". in build settings add , "level1", then add "level2", in build setting you should have two scenes, one called "level1" which will have 0 by it and "level2" which will have a 1 by it. this is the code I would use for something like that, i am showing you this because I know it works, it will count up to 100 then load "level2" which has a level prefix of "1" " var Score : int = 0; var ScoreToAdd : int = 7; var ScoreAddInterval : float = 1;
function Start () {
StartScoreLooping();
} function OnGUI(){ GUI.Box(Rect(100,10,100,25), Score.ToString()); } function StartScoreLooping () { var i = 1; for(i=1;i>0;i++){ yield WaitForSeconds(ScoreAddInterval); Score += ScoreToAdd; if(Score >= 100){ Application.LoadLevel(1); }
}
}" I am pretty sure the Application.LoadLevel("win"); needs to be changed to the integer that is next to the scene "win" in build settings. I hoped this helped you out.
I am pretty sure that variable should be an integer [...] I am pretty sure the Application.LoadLevel("win"); needs to be changed to the integer
No. Application.LoadLevel: "Loads the level by its name or index."
Answer by Statement · Nov 09, 2015 at 07:48 PM
You're never calling WinningScore.
function EachSecond()
{
score=score+addscore;
WinningScore(); // You forgot to call WinningScore!
}