timer

i have a power up, and i want to put a timer on it, i have a code there, but the timer is not working
here’s the code for the player…

#pragma strict
static var attractCoins: boolean = false;
var timer: float = 15.0;

function Start () {
    attractCoins = false;
}

function OnTriggerEnter(info : Collider)
{
    if(info.tag == "Magnet")
    {
        attractCoins = true;
    }
}
function Update () {

    timer -= Time.deltaTime;
   
    if (timer <= 0)
    {
        timer = 0;
        attractCoins = false;
    }
}

function onGUI()
{

    GUI.Box(new Rect(10, 10, 50, 20), "" + timer.ToString("0"));

}

and here’s for the coin script…

#pragma strict
var target : GameObject;

function Start () {
    target = GameObject.FindGameObjectWithTag("Player");
   
}
function Update () {
    if(car.attractCoins == true && Vector3.Distance(transform.position,target.transform.position) < 5.0)
    {
        transform.position = Vector3.MoveTowards(transform.position, target.transform.position, Time.deltaTime * 5);
    }
}

Not working is vague. What exactly is happening? What do you want to happen?

At a glance I see onGUI. This should be OnGUI.

ow yeah sorry, ok first, the OnGUI is not viewing on the game play, second the attractCoins was still collecting coins.

if (timer <= 0)
    {
        timer = 0;
        attractCoins = false;
    }

Sweet.

Changing onGUI to OnGUI will fix the first problem. Scripting is quite finicky when it comes to capital letters.

I don’t see anything obvious that would mess up the end of the attraction. You may need to sprinkle in Debug.Log quite liberally to help you figure out what is happening.

ow thank you BoredMormon it works

@
joytdecastro
For the education of others, can you please post your amended solution ?

Thanks

yeah sure…

#pragma strict
static var attractCoins: boolean = false;
var timer = 15.0;

function Start () {
    attractCoins = false;
}

function OnTriggerEnter(info : Collider)
{

    if(info.tag == "Magnet")
    {
        attractCoins = true;
       
    }
}
function Update () {

    if (attractCoins)
    {
        timer -= Time.deltaTime;
            if (timer <= 0)
            {
                timer = 0;
                attractCoins = false;
            }
    }
}

function OnGUI(){
   

    if (attractCoins)
    {
    GUI.Box(new Rect(10, 10, 50, 20), "" + timer.ToString("0"));
    }
}

timer will probably ALWAYS be less than 0 when you get to the if (timer <= 0) line.

Even though you set it equal to 0 within the IF statement, the next time it goes thru Update() timer will get set to -Time.deltaTime again. So unless each loop thru Update takes 0 time, it will always be set to this time change.