Boolean being constantly redefined?

OK, the title here may be misleading as I'm not TOTALLY sure it's the issue, but it's the only thing i can think of right now. I'm writing a script that allows you to pick up and drop objects, only i cannot get the object to drop at all. Here's the if statement I currently have:

-function Update() {

var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
var LayerMask = 1 << 9;

var otherThing: Transform;
var hasObject = false;

Debug.DrawRay(transform.position, fwd, hit, 50, LayerMask);

if(Input.GetButton ("Fire2")){
   if (hasObject){
   otherThing.rigidbody.isKinematic = true;
   transform.DetachChildren();
   //otherThing.parent = null;
   hasObject = false;
   print("Dropped "+otherThing.gameObject.name+"
");
     } else{
      if (Physics.Raycast (transform.position, fwd, hit, 50, LayerMask)) {
 otherThing = hit.transform;
 otherThing.rigidbody.isKinematic = true;
 otherThing.parent = transform;
 hasObject = true;
 } else{
print ("Nothing to pickup!
");
}

}
}
}

So my object gets picked up, but not put down again. The Object dropped message doesnt display either. I THINK the issue is that because i'm repeatedly defining hasObject as false due to it being in an Update function, but I don't know how to make it only define hasObject at false when the level loads whilst still being able to call it into my update function. Probably quite a rookie problem i admit :)

Thanks for the help!

If you're working in Javascript (which it appears you are), you simply move `var hasObject = false` somewhere in the same file outside the Update function (or any other function). If you're looking for a lengthy read on the concept at play here, Wikipedia has you covered.

Yep, that is exactly what you are doing :) Good catch. Just take all of your variable declarations out of the update function, cause you're re-creating them every frame. Below I did it for you just removing the hasObject boolean from the function. However, you should look through all of the variables you're setting up to make sure you couldn't save time just declaring them once then updating the value each frame.

var hasObject : boolean = false;

funciton Update(){

    var fwd = transform.TransformDirection (Vector3.forward);
    var hit : RaycastHit;
    var LayerMask = 1 << 9;

    var otherThing: Transform;

    Debug.DrawRay(transform.position, fwd, hit, 50, LayerMask);

    if(Input.GetButton ("Fire2")){
       if (hasObject){
       otherThing.rigidbody.isKinematic = true;
       transform.DetachChildren();
       //otherThing.parent = null;
       hasObject = false;
       print("Dropped "+otherThing.gameObject.name+"
");
         } else{
          if (Physics.Raycast (transform.position, fwd, hit, 50, LayerMask)) {
     otherThing = hit.transform;
     otherThing.rigidbody.isKinematic = true;
     otherThing.parent = transform;
     hasObject = true;
     } else{
    print ("Nothing to pickup!
");
    }

    }
    }
    }