Scripting Javascript

Can someone explain to me why this doesn't work and fix it, I want the script to turn a box green and set objective to true when the player presses 'e'. Right now when I use the script nothing happens.

var player : GameObject; 
var range : float=10f;
var distance = Vector3.Distance(transform.position, player.transform.position);
var objective = false;

function PickUp()
{
        if (Input.GetKeyDown("e")) 
        {
                renderer.material.color = Color.green;
                var objective = true;
                print(objective);
        }
}

From your code, it doesn't look like PickUp() is being called anywhere.

Try changing it to:

function Update()
{
    if (Input.GetKeyDown("e")) 
    {
        renderer.material.color = Color.green;
        objective = true;
        print(objective);
    }
}

Update() is automatically called every frame.


I've just noticed that you're also declaring another `objective` variable inside the ifcheck. You've already declared it outside, so you can just assign its value to `true` without the `var` preceding the name.