- Home /
How can I access all game objects connected in a cluster by fixedjoints?
I am new to Unity, but not new to game development. I have been trying for some time now to set up a sort of clustering system for some objects in my game.
Basically, I have multiple moving objects in my world. They will all be of the same object type and will be running the same base script. When they hit one another, they stick to one another and I already have fixed joints being created to hold the objects together. My question is:
How can I, from the script of one object, access each of the other objects which are connected to one another via fixed joints? It seems like an easy problem to solve, but the tricky part is that object1 could be connected to object2, and object2 could be connected to object3 and object4. I need to be able to access all four objects, in code, from any one of the objects. For instance, if one is destroyed, I want to inherently destroy the other three.
Any help is much appreciated. :) P.S. I'm using Javascript.
Answer by Montraydavis · Oct 27, 2012 at 06:27 PM
I would suggesting finding objects of type x.
http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectsOfType.html
function OnMouseDown () {
var hinges : HingeJoint[] = FindObjectsOfType(HingeJoint) as HingeJoint[];
for (var hinge : HingeJoint in hinges) {
if ( hingeConnected ){
code......
}
}
}
Obviously, Joints or w/e needs to be of type (whatever you are wanting), and hingeConnected depends on your way of detecting this... I don't have your code, so, you will have to do that part .
I appreciate the response, though I should have mentioned that there will be more than one cluster going on at the same time between the same object types. Using this method would probably cause problems and destroy or affect joints and objects that are in different clusters, which wouldn't be good. :( I do appreciate the information, however.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082947im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
HELP PLEASE PROBLEM WITH JOITS 0 Answers
consecutive angularxdrive.positionspring.equals() not working 0 Answers
Bend object with physics interaction 0 Answers
Rigidbody hover, bodies connected with joints should hover together. 0 Answers
Problem with joints 0 Answers