- Home /
How to assign a Component to a Public Variable if there are multiple of the same kind?
I have an object with 3 hinge joints, and I need a script to reference to each of them, but it needs to know which hinge joint which. I would prefer something manual(Drag and Drop), but if it is a script, that is fine.
Answer by JVene · Jul 26, 2018 at 02:28 AM
I find a bit of humor here, because Drag and Drop would be the automated method, the script approach would be manual. Sorry, I couldn't resist.
I can predict you'll eventually rethink putting 3 hinge joints on one GameObject, but I could certainly be wrong if your results are as you want. I get a lot more predictable behavior by hosting each such joints on a child GameObject for positioning, which would, of course, change how you get them.
That said, you can get multiple components in an array GetComponents. Given a type, you can a list of all attached components of that type. How you store those in members is up to you, but if your member variable is an array of the appropriate type, the return from GetComponents is all you'd have to do.
Your answer
![](https://koobas.hobune.stream/wayback/20220612171538im_/https://answers.unity.com/themes/thub/images/avi.jpg)