- Home /
really strange thing with "if" statment not working well
i am attaching a scrit in Js which suppose to decide if the new score is a high score or not. at the end of the script I have and "if" statment that suppose to check if the value of the var "hightime" is equal or not to the value of "ctime". even though it is showing trhough the Debug.Log that the values are equal, it is behaving as if they are not. i am also attaching a screen shot of the debug log where it shows that the valuse are the same but it prints some times "Old Highscore" - which suppose to happen only when the valuse are not the same. I have no idea why, or what to do to fix this. any idea will be great!
#pragma strict
var textfield:GUIText;
var hightime : float;
var levelnum : int;
var scoreFiles : TextAsset;
var thisTime : TIMER1;
var index = 0;
var scorestring : String;
levelnum = GameObject.Find("LevelCounter(Clone)").GetComponent(levelCounter).levelNum;
function Start ()
{
textfield = GetComponent(GUIText);
scorestring = PlayerPrefs.GetString("scoreArry");
var score : Array = scorestring.Split (","[0]);
var j = score.length;
var time = GameObject.Find("txt-time").GetComponent(TIMER1);
var ctime = time.levelTimer;
for (var i = 0; i<j; i++)
{
index = i;
if (index+1 == levelnum)
{
var thisTime : String = score[index];
hightime = parseFloat(thisTime);
textfield.text = " = " + hightime.ToString("F2");
Debug.Log ("1. " + score[index]);
Debug.Log ("2. " + hightime);
Debug.Log ("3. " + ctime);
if ( hightime == ctime)
{
Debug.Log ("New Highscore");
var _bestimeNew = UIButton.create ("GFXGold.png", "GFXGold.png" ,0,0);
_bestimeNew.positionFromBottom(0.35,-.15);
}
else if ( hightime < ctime)
{
Debug.Log ("Old Highscore");
var _bestime = UIButton.create ("GFXGoldBW.png", "GFXGoldBW.png" ,0,0);
_bestime.positionFromBottom(0.35,-.15);
}
}
}
}
Add static typing to everything, and then see where you are. I suspect that the problem happens because you are comparing a string with a float- the float that evaluates to '3.14' is not the same as the string "3.14".
Also, you have declared a variable named time. Time is already a built-in class. It ain't generally a good idea to be declaring your variables the same as class, because this can cause ambiguity and thus, screw things around in your code. Try calling it something else.
Answer by fafase · Apr 23, 2012 at 08:33 AM
Am I wrong or you are doing all this in the start fct?
You are only checking once at the beginning but then in the game nothing of it is happening.
But I don't understand the whole thing, what would be the point to check at start the highscore? you'd rather check it at the end, seems pretty logic to me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070630im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why does if(levelToLoad ==null) not work? 2 Answers
If/else statements working one way, and not the other 0 Answers
Setting Scroll View Width GUILayout 1 Answer
not sure whats happened :S 1 Answer
Code not entering if statement. 1 Answer