Hi,
var newJoint:HingeJoint=blocks[i-1].AddComponent(HingeJoint);
newJoint.connectedBody=blocks[i].rigidbody;
newJoint.anchor.y=-0.4;
The above code has effect only once that is the first time the script is executed. The next time the script is executed, the code is executed but has no effect on the gameobject. Also the function Destroy(blocks*.rigidbody.hingeJoint); has no effect.*
Can someone tell whats happening.
possible explanation to the first problem;
The first time you run the script it will add a component (HingeJoint)
the second time you wont be able to add a component since it is already there. Try it manually in the inspector for yourself…
You need to add some code to see if blocks[ ] already has a hingejoint attached, and act accordingly.
The second problem I really don´t know, from the info you supplied, what might be going on.
Hi,
Here is the complete script, The "“LeftBoundary4” is a trigger to which this script is attached.
var blocks: GameObject[];
function OnTriggerEnter(other: Collider)
{
var i:int;
var j:int;
var tempBlock:GameObject;
var xyz:Vector3;
if(this.name=="LeftBoundary4")
{
Destroy(blocks[0]);
for(i=0;i<11;i++)
blocks[i]=blocks[i+1];
xyz=blocks[i].transform.position;
xyz.x=xyz.x+1;
blocks[i]=Instantiate(blocks[1],xyz,blocks[1].transform.rotation);
blocks[i].rigidbody.freezeRotation=true;
var firstJoint=blocks[i-1].GetComponent(HingeJoint);
if(firstJoint!=null)
Destroy(firstJoint);
if(blocks[i-1].rigidbody.hingeJoint==null)
{
Debug.Log("Hingejoint absent on blocks[i-1]");
}
else if(blocks[i-1].rigidbody.hingeJoint!=null){
Debug.Log("Hingejoint present on blocks[i-1]");
}
var newJoint1:HingeJoint=blocks[i-1].AddComponent(HingeJoint);
newJoint1.connectedBody=blocks[i].rigidbody;
newJoint1.anchor.y=0.4;
var newJoint2:HingeJoint=blocks[i-1].AddComponent(HingeJoint);
newJoint2.connectedBody=blocks[i].rigidbody;
newJoint2.anchor.y=-0.4;
var finalJoint1:HingeJoint=blocks[i].GetComponent(HingeJoint);
if(finalJoint1!=null)
{
Destroy(finalJoint1);
Debug.Log("First Hinge Joint Destroyed");
}
var finalJoint2:HingeJoint=blocks[i].GetComponent(HingeJoint);
if(finalJoint2!=null)
{
Destroy(finalJoint2);
Debug.Log("Second Hinge Joint Destroyed");
}
}
}
Note: blocks[ ] hold reference to 12 cube blocks, each cube has two hingejoint attached to it which joins the next cube in the array.
Here the only problem is the second Destroy(finalJoint2); present at the end of the code is not working. I don’t get any errors It enters the if block and also displays the message. The Destroy(finalJoint1); works fine. Now if i remove the Destroy(finalJoint1); then Destroy(finalJoint2); works. All i want to do is destroy the two hingejoints present on a single block.