- Home /
hit.gameObject.AddComponent not working
Hi,
I would like to add a configurable joint to the rigidbody that the raycast is hitting but when I type hit.gameObject.AddComponent ... it doesn't recognize the statement. What am I doing wrong ?
My code :
void Update() {
if(Input.GetMouseButtonDown (1)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)){
Debug.DrawLine(ray.origin, hit.point, Color.cyan);
if(hit.rigidbody != null){
ConfigurableJoint joint = hit.gameObject.AddComponent<ConfigurableJoint>();
joint.xMotion = ConfigurableJointMotion.Locked;
joint.yMotion = ConfigurableJointMotion.Locked;
joint.zMotion = ConfigurableJointMotion.Locked;
joint.angularXMotion = ConfigurableJointMotion.Free;
joint.angularYMotion = ConfigurableJointMotion.Locked;
joint.angularZMotion = ConfigurableJointMotion.Locked;
joint.angularXDrive = new JointDrive
{
mode = JointDriveMode.Velocity,
maximumForce = 10f,
positionDamper = 0f,
positionSpring = 0f
};
joint.breakForce = 20;
joint.breakTorque = 10;
joint.enableCollision = true;
}
}
}
}
Comment
What means "it doesn't recognize the statement"? Build error? Sintax Error?
What's the error message?
It's a syntax error, the code line becomes red and it says : UnityEngine.RaycastHit does not contain a definition for 'gameObject'
You should access the gameobject through the collider component like this:
ConfigurableJoint joint = hit.collider.gameObject.AddComponent<ConfigurableJoint>();
Best Answer
Answer by giulio-pierucci · Mar 02, 2015 at 04:50 PM
ConfigurableJoint joint = hit.collider.gameObject.AddComponent();