- Home /
Level advancment Scirpt
hey guys, I'm making a fps, and am making a script that automatically goes to the next level when all enemies are dead. it goes like this:
var target : Transform;
function Update ()
{
if(target == null && GameObject.FindWithTag("Robot") == null);
Application.LoadLevel(1);
}
the problem is than when i attach the script to a game Object (Ive tried different objects) and play, it shows level 0 for 2 seconds, then loads level 1!
Ive tried modifying the tag and using Application.LoadLevel(Application.loadedLevel + 1); but it still doesnt work!
please help
Answer by · Aug 18, 2010 at 04:53 AM
You had a semicolon after the if, so it wasn't doing anything after the ifcheck. The Application.LoadLevel would always occur. Try this:
var target : Transform;
function Update ()
{ if(target == null && GameObject.FindWithTag("Robot") == null) { Application.LoadLevel(1); } }
Another thing to be wary of is that the Find/FindWithTag functions are quite intensive, and should generally not be used in Update() - as it is, you're searching every GameObject for one with the "Robot" tag every frame. You'd be much better off putting that in a seperate function and calling it when an enemy dies (to check if there are any left). For example:
function CheckForEnemies()
{
if (GameObject.FindWithTag("Robot") == null)
{
Application.LoadLevel(1);
}
}
You'd then need to hook this function up to where you apply damage. For example:
function ApplyDamage(damage : int) { this.health -= damage; // apply the damage that has been passed through
if ( this.health <= 0 ) // if the enemy is now dead (<=0 health remaining) { CheckForEnemies(); } }
This is just an example - there may be a better way to hook it up, depending on the design/structure of your game. Other things to consider would be changing the LoadLevel from a static value to use a variable. You could have a "gameManager" object that stores a 'static variable' for the currentLevel, and then you could Application.LoadLevel(currentLevel+1), or put it in a function that checked if it was the last level, etc. Anyway, hopefully this will get you started.
ok, that sounds much more organized, but how would i do that? oh, and thanks for the tip about the extra semicolon. also how would i make it fade? i have a "LevelLoadFade" script, but where would i attach that?
I've updated the answer to include some examples. Without knowing how you've structured your data, I can only guess at how you'd best approach implementing a solution! Hope this helps.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Basic scripting help (FPS Tutorial) 1 Answer
Next lvl code help 1 Answer