- Home /
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+"\n"); } 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!\n"); }
} } }
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!
Answer by burnumd · Mar 08, 2011 at 02:29 PM
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.
Answer by Bob5602 · Mar 08, 2011 at 02:32 PM
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+"\n");
} 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!\n");
}
}
}
}
Hey, after doing this I think it's almost working, but I still get an error message saying "NullReferenceException: Object reference not set to an instance of an object" referring to this line:
otherThing.rigidbody.is$$anonymous$$inematic = true;
Any ideas?
Don't worry if not, I'll keep working at it and post a seperate question later if I can't figure it out.
Hmm. Your otherThing is a transform, so it won't have a rigidbody component. So you can't call otherThing.rigidbody because it doesn't exist, thats where your error is co$$anonymous$$g from. How to fix it is more tricky, since hit is a raycasthit which isn't a gameobject and has limited properties. In my experience with similar things, I would make otherThing a GameObject, and set it as the thing you hit using some find searching for the hit.name and matching them. Works if you have uniquely named objects, or you can rename it to something unique for the purposes of these actions. (more...)
... However, it appears you are setting is$$anonymous$$inematic = true on both accounts. Is it ever false? if not, you don't really need those statements in there. Or, you could keep it as it is, and set the is$$anonymous$$inematic property on the object somewhere else. However, I have other problems with this script since you're not actually changing the object you hit at all, but are changing this 'otherThing' which is re-created every frame as a temporary transform. The script wouldn't do anything to any gameobject you run into.