Can anyone explain why this error? BCE0051: Operator '-' cannot be used with a left

BCE0051: Operator ‘-’ cannot be used with a left hand side of type ‘float’ and a right hand side of type ‘Object’. It is on line TimerTrack1.js(34,21)

 var startTime;
 var restSeconds : int;
 var roundRestseconds : int;
 var displaySeconds : int;
 var displayMinutes : int;
var guiskin: GUISkin;
private var guienable:boolean=true;
//Just provide any value here and other buttons will be set accordingly
static var buttonWidth: int=200;
static var buttonHeight: int=50;
var f : Font;

var clip : AudioClip;

var countDownSeconds : int;







function Awake()
{
startTime = Time.time;


}


function OnGUI()
{

guiTime = Time.time - startTime;

restSeconds = countDownSeconds - (guiTime);


	GUI.skin=guiskin;
	GUI.skin.font=f;	
	
	if(GUI.Button(Rect(100,20,buttonWidth,buttonHeight),"Restart Level"))
	{
		LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 0.5);
	}	


if(restSeconds == 60)
{
print ("One Minute Left");
audio.Play();
}



if(restSeconds == 0)
{
print("Time is Over");

Application.LoadLevel(10);
}
GUI.color = Color.white;
roundRestseconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundRestseconds % 60;
displayMinutes = roundRestseconds / 60;

var text: String;
text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
GUI.Label (Rect (100, 500, 100, 100), text);

}

Does changing var startTime; to var startTime : flaot = 0.0; change anything ?

I may be wrong but…

  1. guiTime is not declared at all
  2. Technically not an error in Unityscript but you should declare guiTime and startTime as floats

By the way, for the next line of code, you may lose a bit of accuracy for going from float to ints directly.

I tried that and I am still getting the same error. However, when I build for the PC version, the code works. However, with Android, that is were I am receiving my problems.

You may want to go to the Android forum and post this again (just in case as this is an Android related problem… by the way, where do you get this error? In the Unity console?) but you may try these things as I’ll assume the Android framework is different in some way that it’s stricter:

  1. Declare every variable for sure (guiTime and restSeconds) and make sure EVERY variable has an explicit var type
  2. Make sure startTime also is a float

I’ve moved onto to C# recently (now that I’m working solely on my Android game and have stopped mostly following a very helpful book that uses nearly only JS), but that error seems quite basic: The operator “-” can’t subtract a float and a general Object- it thinks startTime is an object.

Thanks. I am moving over to C# too. I think C# works better with Android platform than Java Script

Well, good luck! Beware that many useful scripts are in Java Script so you will definitely get experience converting to C#

Also, by the way, all you have to do to fix this problem is declare startTime as a float. Just to be clear so you don’t have to scrap this script at the moment :slight_smile: