- Home /
Why this script is not working?
Hi,i have script,i wanted to make it that i can only press "n" button from a certain distance,but its not working,why,please check it out and help me ;(
var myDistance: float = 20;
var cube : GameObject;
var jointExist : boolean;
function OnCollisionEnter(c : Collision) {
if(!jointExist){
var joint = gameObject.AddComponent(FixedJoint);
joint.connectedBody = c.rigidbody;
jointExist = true;
}
}
function Update(){
if(Input.GetKeyDown(KeyCode.N)){
if(Vector3.Distance(transform.position, cube.transform.position) < myDistance){
if(!jointExist){
var joint = gameObject.GetComponent(FixedJoint);
Destroy(joint);
jointExist = false;
}
}
}
}
What exactly isn't working? Are you getting any errors in the console? If not, add Debug.Log messages right after Get$$anonymous$$eyDown to see what the variable values are...
Debug.Log("Distance=" + Vector3.Distance(transform.position, cube.transform.position));
Debug.Log("myDistance=" + myDistance);
Debug.Log("joinExists=" + jointExists);
Do the values make sense? Are they what you expect? Do they satisfy the conditionals so the code inside the varous if statements executes?
Is distance 20 in inspector as well? I've messed up thinking I got value in script many times.
Answer by Gizmoi · Jan 25, 2013 at 08:34 PM
I'm not 100% sure what this script is attempting to do, but I imagine that in Update, the 3rd if statement should read:
if (jointExist == true)
rather than checking if it's false.
Well it should work that when i press "n" the cube gets sticky and it stick to any collision.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Raycast Destroys player. 1 Answer
How To Invoke RayCastAll? 1 Answer
Problems Scripting a Camera 1 Answer
NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer