Referencing hingejoint in a script using variable does not work

Hi, Guys,

This is my third or fourth post related to my issue. I have received no replies to my previous posts.

tempHinge1=blocks[i-1].AddComponent(HingeJoint);
	tempHinge2=blocks[i-1].AddComponent(HingeJoint);
	tempHinge1.rigidbody.hingeJoint.connectedBody=blocks[i].rigidbody;
	tempHinge2.rigidbody.hingeJoint.connectedBody=blocks[i].rigidbody;

In the above code, the first two line works that is i can add two hingejoints. but i cannot reference those added hingejoint through the variables tempHinge1 and tempHinge2. The blocks[ ] is an array that is referencing to 12 cube game objects which i have set through the editor.

Thanks.

I would try
var tempHinge1 : HingeJoint = blocks[i-1].AddComponent(HingeJoint);

I tried it your way and it still does not work. The thing is, the second hingejoint cannot be access through the variable i declare. I am giving my code below. In the code i tried creating two hinges and tried to destroy two hinges of same object. I can only refer and destroy only one hinge joint with or without variable.

var tempHinge1:HingeJoint;
var tempHinge2:HingeJoint;


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;
	//if(blocks[i-1].rigidbody.hingeJoint==null)
	//{
	var tempHinge1 : HingeJoint = blocks[i-1].AddComponent(HingeJoint); 
	var tempHinge2 : HingeJoint = blocks[i-1].AddComponent(HingeJoint); 
	tempHinge1.rigidbody.hingeJoint.connectedBody=blocks[i].rigidbody;
	tempHinge2.rigidbody.hingeJoint.connectedBody=blocks[i].rigidbody;
	//}
	
	//blocks[i-1].rigidbody.hingeJoint.connectedBody=blocks[i].rigidbody;
	//blocks[i-1].rigidbody.hingeJoint.anchor.y=0.5; 
	//blocks[i-1].rigidbody.freezeRotation=true;
	
	
	
	//blocks[i-1].rigidbody.freezeRotation=true;
	if(blocks[i].rigidbody.hingeJoint!=null)
	{
	Debug.Log("Destroyed");
	Destroy(blocks[i].rigidbody.hingeJoint);
	Destroy(blocks[i].rigidbody.hingeJoint);
	}
	
	
  }

I have only attach relevant code. What am i doing wrong?

Are you saying that the two variables end up referring to the same hinge joint component (so you destroy one and the other just refers to the destroyed hinge)? To check this, try changing the code as follows:-

var tempHinge1 : HingeJoint = blocks[i-1].AddComponent(HingeJoint);   // Existing declarations.
var tempHinge2 : HingeJoint = blocks[i-1].AddComponent(HingeJoint);

if (tempHinge1 == tempHinge2) {
  print("Both hinges are the same");
} else {
  print("Hinges are different");
}

if (tempHinge2 == null) {
  print("Second hinge is null");
}

See what you get from this. There may be a bug (or even an intentional feature) that prevents two hinges from being added to one object but the actual behaviour may not make this clear.

Hi andee, i tried the code you mentioned and got the following result

Hinges are different
UnityEngine.MonoBehaviour:print(Object)
OnSliding:OnTriggerEnter(Collider) (at Assets/Custom Scripts/OnSliding.js:54)

Also i tried some other things with the following code,

var tempHinge1 : HingeJoint = GameData.Strip4Blocks[i-1].AddComponent(HingeJoint);   // Existing declarations.
var tempHinge2 : HingeJoint = GameData.Strip4Blocks[i-1].AddComponent(HingeJoint);
tempHinge1.anchor.y=0.4;
tempHinge2.anchor.y=-0.4;

//var finalJoint1:HingeJoint=GameData.Strip4Blocks[i-1].GetComponent(HingeJoint);
//Destroy(finalJoint1);
//var finalJoint2:HingeJoint=GameData.Strip4Blocks[i-1].GetComponent(HingeJoint);
//Destroy(finalJoint2);
//Destroy(tempHinge1);
//Destroy(tempHinge2);

if (tempHinge1 == tempHinge2) {
  print("Both hinges are the same");
} else {
  print("Hinges are different");
}

I tried turning on and off the commented statements and it seems that tempHinge1 and tempHinge2 work properly. But with variables finalJoint1 and finalJoint2 only first one works. The problem is with GetComponent() i think. In inspector also the second joint remains.