Treasure chest problem

So I have this script for opening a treasure chest but i only want it to work once then stop.

function Update ()
{
if ( Input.GetButtonDown(“Fire1”) ) {
transform.animation.Play();
}
}

Because if i click more than once it continues to open.
Just want it to be able to click on it only once.

and also how do i change it so i push tne key x to activate this procedure? :slight_smile:

use a boolean variable to keep track of your chests state and Input.GetKeyDown() for keyboard input.

var hasBeenOpened:bool = false;

function Update()
{
    if(!hasBeenOpened && Input.getKeyDown(KeyCode.X))
    {
        hasBeenOpened = true;
        transform.animation.Play();
    }
}

You should use boolean variables to control this:

var open = false;
var activated = false;

function Update(){
  if (Input.GetKeyDown("x")){
    activated = true;
  }
  if (activated && !open && Input.GetButtonDown("Fire1")){
    animation.Play();
    open = true;
  }
}

You must first press X to activate - this sets activated to true, and enables the second if, which in turn only accepts the button Fire1 when the chest isn’t open yet. Once open is true, the second if doesn’t play the animation anymore.

You can store the state of the box in a boolean named ‘isOpen’ or similar. In the update method you then check, if the box is closed or open.

Thanks heaps guys, worked out :slight_smile: finally
Really appreciated :slight_smile: