HELP reducing a value overtime

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 );
     
}

thanks

jordan

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 );
}

is that going to work with the var as an int (untyped var without a decimal point or an f => int)? 100 - 0.000002 = 99 or 100 - 0.00002 = 100?

var score = 100.0;

function Update()
{
    score -= Time.deltaTime * 2.0;
}

function OnTriggerEnter( other : Collider )
{
    Debug.Log("OnTriggerEnter() was called");

    if (other.tag == "fuel")
    {
        Debug.Log("Other object is a coin");
        score += 5.0;
        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 );
}

I suppose you could cast the score as an int when you display it in the GUILayout.Label if you didn’t want to have the decimal.

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
}
var score = 100.0;

function Update()
{
    score -= Time.deltaTime * 2.0;
}

function OnTriggerEnter( other : Collider )
{
    Debug.Log("OnTriggerEnter() was called");

    if (other.tag == "fuel")
    {
        Debug.Log("Other object is a coin");
        score += 5.0;
        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 = " + Mathf.FloorToInt(score));
}

Or Mathf.CeilToInt()

1 Like

It can still be a float and be displayed as an Int. CeilToInt is a good choice, but a bit more memory intensive than what I would suggest…

GUILayout.Label( "fuel = " + score.ToString("F0"));

“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.

Nice solution to the problem!

thanks man it worked great