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.
Cheers!
Thanks $$anonymous$$mmpies, But by that I still need to drag in the object, and when I drag in the GameObject it takes the first SpringJoint every time. :(
I can drag in just the spring joint component, can I? Because I can't have two inspector views. Am I missing some something?
Thanks a lot for quick answer!
Got it... I just needed to have a script on the actual GameObjects the springs are attached to and now I can see the custom variables + the spring joints them selves! Thank you $$anonymous$$mmpies, AWESO$$anonymous$$E! Hour long struggle is over!
Glad you got it sorted :)
EDIT - incidentally it's just one way of doing it. You could also have added a unique tag or name to them. There's always lots of ways of cracking a particular program$$anonymous$$g nut in Unity.
Answer by Mmmpies · May 28, 2016 at 04:26 PM
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. :D
Your answer
Follow this Question
Related Questions
Is it possible to leave a comment on components like prefabs? 1 Answer
Use c# "ref" with a GameObject or Component 1 Answer
How to get component value without using GetComponent() and Update()/FixedUpdate()? 1 Answer
do something every time a Sprite is added to the Scene 1 Answer
Animation clip not playing but still affecting components of other clips 0 Answers