< operator problem

the problems is this:

i have 2 array for check the best time in race, the first array take the current time values, the second take the best time data stored on playerpref, all value are int,

if i use :
if (lastlap[0] < bestlap[0]){

on unity 2.5 works, on unity iphone i take this error:
Assets/Scripts/Player/Moto_controller.js(632,118): BCE0051: Operator ‘<’ cannot be used with a left hand side of type ‘Object’ and a right hand side of type ‘Object’.

else if i use if (lastlap[0] == bestlap[0]){ i dont take error

p.s. I’m an artist pls help me with an commented exaple :smile:
sry for bad english.

you need to store times in the arrays. obviously you are storing objects in there which is causing the problem.

the comparision works because its defined on objects (and it would likely always fail unless its the completely same object)

I think you need to type your arrays (ie make them floats), or cast the values before comparing them.

mmm i dont understand where is the problem , i post a better example

var minutes = 0;
var seconds = 0;
var fraction = 0;
var startTime = 0.00; 

var go : boolean = false;

var UltimoGiro = new Array ();
var MigliorGiro = new Array ();
var playTime = 0.0;

function Start(){

	 UltimoGiro.Push (minutes);
	 UltimoGiro.Push (seconds);
	 UltimoGiro.Push (fraction);
	 
	 SetRecordTempo();
}

function Update(){

	playTime = Time.timeSinceLevelLoad - startTime;
        minutes = (playTime/60 );
        seconds = (playTime % 60);
        fraction =  ((playTime *10) %10);
        Timer.guiText.text = String.Format ("Time :				{0:00}:{1:00}:{2:00}", minutes, seconds, fraction); 
}

////// this function run without problems
function SetRecordTempo(){	
			 //////// tempo pista 01
	 if (Application.loadedLevel == 5){  ////////////// se è la pista 01
			MigliorGiro.Push (PlayerPrefs.GetInt("pista01Minuti"));
			MigliorGiro.Push (PlayerPrefs.GetInt("pista01Secondi"));
			MigliorGiro.Push (PlayerPrefs.GetInt("pista01Decimi"));
	 
	 
		if (MigliorGiro[0] == 0  MigliorGiro[1] == 0  MigliorGiro[2] == 0){
			PlayerPrefs.SetInt("pista01Minuti",02);
			PlayerPrefs.SetInt("pista01Secondi",00);
			PlayerPrefs.SetInt("pista01Decimi",00);
			}
			
		MigliorGiro[0] = PlayerPrefs.GetInt("pista01Minuti");
		MigliorGiro[1] = PlayerPrefs.GetInt("pista01Secondi");
		MigliorGiro[2] = PlayerPrefs.GetInt("pista01Decimi");

		BestTime.text = String.Format ("Best Time :	{0:00}:{1:00}:{2:00}", MigliorGiro[0],MigliorGiro[1],MigliorGiro[2]);
	}
}

/// this function have problems
function RecordTempo(){
			
			UltimoGiro[0] = minutes;
			UltimoGiro[1] = seconds;
			UltimoGiro[2] = fraction;

			
	 if (Application.loadedLevel == 5){           //////////////////// se  è la pista 01
	 		MigliorGiro[0] = PlayerPrefs.GetInt("pista01Minuti");
			MigliorGiro[1] = PlayerPrefs.GetInt("pista01Secondi");
			MigliorGiro[2] = PlayerPrefs.GetInt("pista01Decimi");
	 
			if (UltimoGiro[0] < MigliorGiro[0]){
				PlayerPrefs.SetInt("pista01Minuti", UltimoGiro[0]);
				PlayerPrefs.SetInt("pista01Secondi",UltimoGiro[1]);
				PlayerPrefs.SetInt("pista01Decimi",UltimoGiro[2]);
				BestTime.text = String.Format ("Best Time :	{0:00}:{1:00}:{2:00}", MigliorGiro[0],MigliorGiro[1],MigliorGiro[2]);
			}
			else if (UltimoGiro[0] == MigliorGiro[0]  UltimoGiro[1] < MigliorGiro[1]){
				PlayerPrefs.SetInt("pista01Minuti", UltimoGiro[0]);
				PlayerPrefs.SetInt("pista01Secondi",UltimoGiro[1]);
				PlayerPrefs.SetInt("pista01Decimi",UltimoGiro[2]);
				BestTime.text = String.Format ("Best Time :	{0:00}:{1:00}:{2:00}", MigliorGiro[0],MigliorGiro[1],MigliorGiro[2]);
			}
			else if (UltimoGiro[0] == MigliorGiro[0]  UltimoGiro[1] == MigliorGiro[1]  UltimoGiro[2] < MigliorGiro[2]){
				PlayerPrefs.SetInt("pista01Minuti", UltimoGiro[0]);
				PlayerPrefs.SetInt("pista01Secondi",UltimoGiro[1]);
				PlayerPrefs.SetInt("pista01Decimi",UltimoGiro[2]);
				BestTime.text = String.Format ("Best Time :	{0:00}:{1:00}:{2:00}", MigliorGiro[0],MigliorGiro[1],MigliorGiro[2]);
			}
		}
	}

Smag what do you mean with "cast the values"  im a total noob programmer (i'm artist),

Well, I don’t know unityscript, but in C# you would do it like this :

(float)yourArray[index]

That tells the compiler what type the value in the array is.

Or you need to do it in the declaration of the array, but not sure if you can do that in unityscript…

In unityscript casting is done through the second casting option from C#

(yourArray[index] as float)

I see, the same as in AS3 then. Makes sense.

sry guys but i dont know c#, is already hard for me to learn unity javascript, any way for make it on javascript?

Yeah, dreamora just posted it…

is not more clear for me, is like bestlap[0] : float ?

i need a pratical example,
thx

No, it’s

(bestlap[0] as float)

Javascript on the iPhone requires explicit typing. The error you got is because the arrays are not typed.

In your code, declare the last lap and best lap arrays as follows:

var lastlap: float[];
var bestlap: float[];

I assume you intend to populate these arrays, so you’ll need to construct them of the needed size like this:

var foo: float[] = new float[n];

Where n is the intended size of the array, assuming this is known at design time.

Or you can use an instance of the Java script Array type, which can be dynamically allocated as items are added to an instance of it. This object can then be converted it to a built-in (i.e. Mono C#) array like this:

var lastlap: float[];

.......

var templastlap = new Array();
templastlap.Push(lastLapTime);
lastlap = templastlap.ToBuiltin(float);

The equality expression works because, it is valid to ask whether two Objects are the same. The inequality operator is valid only for numeric types. In Unity, an attempt will be made to cast each object to a numeric type when evaluating the inequality. Dynamic type casting is not available in Unity iPhone so this fails.

thx Hanultech :slight_smile: now is clear for a non programmer like me:)