Loadlevel isn't working.
Could someone help me with my script? I made a score script. When the score = 1, the scene will be reloaded. But when the score = 3, it needs to load a other level. Right now it will always load the same scene.
This is my script so far.
#pragma strict
static var currentScore : int = 0;
var test = 0;
function Start () {
currentScore = 0;
}
function Update () {
test = currentScore;
}
function OnTriggerEnter (col : Collider) {
if(col.gameObject.name == "Cylinder" || test == 3){
Application.LoadLevel("1v2");
}
if(col.gameObject.name == "Cylinder" || test == 1){
Application.LoadLevel(Application.loadedLevel);
}
}
Answer by Cepheid · Nov 09, 2015 at 05:06 PM
The issue is most likely arising because you are using a short circuit "OR" operator whilst comparing the same string. A short circuit OR will evaluate the first condition and if it is true it will execute the code block and completely ignore the other conditional. Now, because you are asking the compiler to compare the collider's name first it will become true and ignore the test conditional and because you are comparing the same string twice the compiler will be picking both of the statements but one will execute before the other. To fix this issue you'll most likely want to use the && (and) operator and make use of else if instead. For example:
if(gameObject.name == "Cylinder" && Test == 3)
{
Application.LoadLevel("1vs2");
}
else if(gameObject.name == "Cylinder" && Test == 1)
{
Application.LoadLevel(Application.LoadedLevel);
}
This will now compare both the collider's name and the Test score, before executing the code block. I hope this helped. :)