- Home /
Score / Loading Level
I have a level that requires 10 points to win. Right now, I can get 10 points, but the next level does not load. Can anyone tell me how to script a level to load upon completion of the score reaching 10? All help is greatly appreciated. Thank you!
Shawn
Updated with Shawns code in comment reply:
I am lost, here is my code:
var score = 0;
function Update() { if (score > 4) { Application.LoadLevel(1); } }
And this is my score script:
var cam : GameObject; var Win : GameObject; var Lose : GameObject; var score = 1;
function AddToScore() { ++score; guiText.text = "Score: " + score.ToString(); }
function SubtractFromScore() { --score; guiText.text = "Score: " + score.ToString(); }
~Statement
Answer by Statement · Mar 08, 2011 at 04:49 PM
You can do something like this if your levels are next to each other by index:
// if score is 10 or more
if (score >= 10)
{
// if we aren't at the last level
if (Application.loadedLevel != lastLevel)
{
// then load the next level
Application.LoadLevel(Application.loadedLevel + 1);
}
else
{
// Game completed!
}
}
Hey that code seems to work, but it takes me to a new camera in my current scene ins$$anonymous$$d of loading a new level. I even made sure the layers we included in the Build Settings. Any suggestions? Can it interfere with other scripts? It seems to be conflicting with my player character prefab.
Thanks and I appreciate the response!
I doubt it would mess up too much with your other scripts, unless you got something set DontDestroyOnLoad maybe. $$anonymous$$ake sure you've built your scenes so that all game levels are sequential (that level2 is after level1 and so on).
Ya I still can't go from Level 1 to Level 2 when I score points. I am lost, here is my code: var score = 0;
function Update(){ if( score > 4){ Application.LoadLevel(1); }
}
And this is my score script: var cam : GameObject; var Win : GameObject; var Lose : GameObject; var score = 1;
function AddToScore () {
++score;
guiText.text = "Score: " + score.ToString ();
}
function SubtractFromScore(){
--score;
guiText.text = "Score: " + score.ToString ();
}
Any other suggestions? Thank you. I appreciate the help.
Velketor
It's also spawning hundreds audio listeners and I have to re-load Unity.
Please place code in your question. It's unreadable in comments. $$anonymous$$ake sure to format it with the code button (I'll do this for you this time). What goes on in your project is out of my comprehension. No code instantiate anything, and you're loading level 1 in your posted code, not any subsequent levels. Be sure to try this out in a separate project to see it's not this code that mess anything up.
Answer by Safgril · Mar 09, 2011 at 01:47 PM
I am far from an expert. I am new to programming and still trying to figure out how to get my scores to carry over. But i found a cheating quick fix. this will do till you find a better way.
function Update () {
if(playerScore ==2000){
Application.LoadLevel(3);
playerScore = 2001;
}
if(playerScore ==4001){
print("Level 2 complete");
Application.LoadLevel(4);
playerScore = 4002;
}
if(playerScore == 6002){
print("Level 3 complete");
Application.LoadLevel(5);
playerScore = 6003;
}
if(playerScore == 8003){
print("Level 4 complete");
Application.LoadLevel(6);
playerScore = 8004;
}
if(playerScore == 10004){
print("Level 5 complete");
Application.LoadLevel(7);
playerScore = 0;
}
if(playerLives <= 0){
Application.LoadLevel(8);
playerScore = 0;
}
}
function OnGUI(){
GUI.Label(Rect(10,10,200,50), "Score: " + playerScore);
GUI.Label(Rect(10,30,200,50), "Lives: " + playerLives);
}
just adjust the scores to your own levels... and change the load level to what yours are.
Don't feel afraid to answer questions if you are new to program$$anonymous$$g. :) We all start somewhere, and we can all contribute our own thougts and ideas on problems, just like you did. One of the greatest things with answering to questions here is when you realize you're wrong. I have learnt so much from other people correcting my invalid assumptions. And I learn a great deal about other peoples answers. In short; Welcome. :)
I just read my own comment and it might sound a bit snappy. I didn't imply your answer is wrong at all. I meant, even a wrong answer yield something good!
I'll try this out tonight and let you know if it works =) Thanks for the help everyone! I appreciate it.
$$anonymous$$
Your answer
Follow this Question
Related Questions
Load a random scene from a list of strings. 2 Answers
help please: Level Changing Scipt 1 Answer
FPSPlayer script from FPS tutorial won't work... 0 Answers
linking levels? 2 Answers
Loading Level Async without switching the level immediately 5 Answers