PROBLEM SOLVED THANKS GUYS
i have a code that i wrote (below) ive been playing around with it for a while and i just cant figure out how to reduce “fuel” over time. any ideas?
var score = 0;
function OnTriggerEnter( other : Collider ) {
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "fuel")
{
Debug.Log("Other object is a coin");
score += 5;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
}
function OnGUI()
{
GUILayout.BeginArea (Rect (Screen.width/100, Screen.height/100, 100, 750));
GUILayout.Label( "fuel = " + score );
}
You can either, use update to decrement the score(your fuel i guess) or you could setup a Coroutine to trigger every x.x seconds with a condition in the coroutine that checks the score(fuel) for <= 0, below is the former.
Also all you’re trying to do is count down, if you search ‘unity countdown timer’ in google, you will find a examples’o plenty.
var score = 100;
function Update()
{
score -= Time.deltaTime * 2;
}
function OnTriggerEnter( other : Collider )
{
Debug.Log("OnTriggerEnter() was called");
if (other.tag == "fuel")
{
Debug.Log("Other object is a coin");
score += 5;
Debug.Log("Score is now " + score);
Destroy(other.gameObject);
}
}
function OnGUI()
{
GUILayout.BeginArea (Rect (Screen.width/100, Screen.height/100, 100, 750));
GUILayout.Label( "fuel = " + score );
}
No, it isn’t. The easiest solution is to make your variable a float (and then, if you want to display it as an integer, round it for display purposes).
However, if for some reason you need it to be an int, then you’ll need to instead keep track of the time (as in, Time.time value) at which it should next be decremented. In your update method, you’d have something like this:
while (Time.time > nextDecTime) {
score--;
nextDecTime += 0.5f; // or however often you want score to be decremented
}
“F0” is a string manipulator for floats. What it means is that it will show 0 numbers past the decimal place.
For Example:
If you have a float named m_Float with a value of 5.2439:
Debug.Log(m_Float.ToString("F2"); /// result is 5.24
Debug.Log(m_Float.ToString("F1"); /// result is 5.2
Debug.Log(m_Float.ToString("F3"); /// result is 5.243
///etc...
In other words, the number after the “F” is how many spaces from the decimal that will actually show up in your string.