- Home /
Codeing Issues making a Timer
ok i am in the middle of a Game Engines class in a Community College Video Game Development Program and this project im working on wants me to have a timer...i used the code from the First game Engines class thinking that would make it Easy to bang out...now unity seems to have some issues with this JavaScript Code here are the errors
" Cannot convert 'String' to 'System.Type' "... " Operatpr '+' cannot be used with a left hand side of type 'System.Type' and a right hand side of type 'int' "
and this is the full code:
static var strTime : String;
static var timeCount : float = 0.0;
var intSec : int = 0;
var intMin : int = 0;
var strSecZeroFill = String;
var strMinZeroFill = String;
function FixedUpdate ()
{
strSecZeroFill = "";
strMinZeroFill = "";
timeCount += Time.deltaTime;
intSec = Mathf.FloorToInt(timeCount)%60;
intMin = timeCount / 60;
if(intSec < 10)
{
strSecZeroFill = "0";
}
if(intMin < 10)
{
strMinZeroFill = "0";
}
strTime = strMinZeroFill + intMin + " min " + strSecZeroFill + intSec + " sec";
print(strTime);
}
What in the world is going on with this?
bool isTimeUp = false;
float timeLimit = 3;
float currentTime = 0;
void Update(){
currentTime += Time.deltaTime;
if(currentTime > timeLimit){
isTimeUp = true;
//do this if you wanted to reset the timer
currentTime = 0;
isTimeUp = false;
}
}
im not looking for a Time Limit it is just reporting how long they are taking
I've reformatted your code. Please format your code next time with the "101010" button, otherwise its very hard to read. Also, posting the line number for the error is helpful so that we don't have to search through your code to find the problem.
For example:
Assets/Scripts/NewBehaviourScript.js(29,30): BCE0051: Operator '+' cannot be used with a left hand side of type 'System.Type' and a right hand side of type 'int'.
(29, 30)
is the line numbers where your error might be located
Then just use the
currentTime += Time.deltaTime;
and that will give you time.
Answer by hoy_smallfry · Nov 12, 2013 at 12:08 AM
Your problem is
var strSecZeroFill = String;
var strMinZeroFill = String;
should be
var strSecZeroFill : String;
var strMinZeroFill : String;
The equals says it the value is String
which makes the variable a System.Type
. Something like ""
would make it a String
variable.