How in C# do I change value on two same type components, each different value.

Hi guys,
I have one GameObjec, to it I have attached two SpringJoints. In code I want to change each SpringJoints Vector3 position like this:

riderTorso.GetComponent<SpringJoint> ().connectedAnchor = new Vector3 (-0.112f, 0.2f, 0f);

This changes the value of the first SpringJoint attached to this object. I read about the GetComponents(type:SpringJoint) function however I do not understand how to further differentiate each SpringJoint and add unique values (or code) to each.

Sorry if it’s retarded question, and I did rtfm but didn’t find any example where I could differentiate and apply unique code to each SpringJoint.


Just create 2 public SpringJoints in the script. When you look at the object the script is attached to in the inspector you’ll see the SpringJoint entries with a slot next to them, just drag one Springjoint on the first one and the other on the second and you now have a reference to each one. Call them what you want but in this example I call them SJ1 and SJ2.

	public SpringJoint SJ1;
	public SpringJoint SJ2;

Then your code for altering one of them is this…

SJ1.connectedAnchor = new Vector3 (-0.112f, 0.2f, 0f);

Unity’s great for these kind of shortcuts. :smiley: