- Home /
Change multiple hing Joints with a single script?
It seems you can't use
HingeJoint hinge = GetComponentInChildren<HingeJoint>();
and then use
foreach (HingeJoint joint in hinge)
to effect all hinges nested under a parent object as HingeJoint doesn't contain a public instance definition for GetEnumerator. So how can an Enumerator or list be made to solve this?
Say you have a chain connected to the floor and ceiling and you want it to move a little so you set limits.min and max ( https://docs.unity3d.com/ScriptReference/HingeJoint-limits.html) but then you want the chain to break and change all the joints limits.min and max with a single script. Or you have a double door and you want to lock it and then unlock it by changing the joints limits for both doors rather then each one individually. How would you go about making the script capable of using GetComponentInChildren? Or is there a better way I'm not thinking of?
EDIT:
I tried the HingeJoint hinges[] array with no luck and now even just trying to swap single joints at a time calling the functions below with a trigger event isn't working?
using UnityEngine;
public class HingLockControl : MonoBehaviour
{
public bool locked = true;
public float lockedMinLimit = 0;
public float lockedMaxLimit = 0;
public float lockedBounciness = 0;
public float lockedBounceMinVelocity = 0;
public float unlockedMinLimit = -90;
public float unlockedMaxLimit = 90;
public float unlockedBounciness = 0;
public float unlockedBounceMinVelocity = 0;
HingeJoint hinge;
JointLimits limits;
void Start()
{
hinge = GetComponent<HingeJoint>();
limits = hinge.limits;
hinge.limits = limits;
hinge.useLimits = true;
if (locked)
SetLockedHingeLeeway();
else
SetUnLockedHingeLeeway();
}
public void SetLockedHingeLeeway()
{
locked = true;
// Set the hinge limits when locked.
limits.min = lockedMinLimit;
limits.bounciness = lockedBounciness;
limits.bounceMinVelocity = lockedBounceMinVelocity;
}
public void SetUnLockedHingeLeeway()
{
locked = false;
// Set the hinge limits when unlocked.
limits.min = unlockedMinLimit;
limits.bounciness = unlockedBounciness;
limits.bounceMinVelocity = unlockedBounceMinVelocity;
limits.max = unlockedMaxLimit;
}
}
Answer by darksider2000 · Apr 08 at 11:14 PM
In your case hinge is not an array. This would work:
HingeJoint[] hinges = GetComponentsInChildren<HingeJoint>();
Note the added 's' in GetComponentsInChildren You can then use the foreach loop.
I would do it like this:
foreach (Transform child in transform) {
if(child != transform) {
HingeJoint hinge = child.GetComponent<HingeJoint>();
// Do stuff with hinge here
}
}
No, I had tried an array and you get errors about a "field initializer cannot reference the non-static field, method or property" and the hinges.limits throws errors as the HingeJoint[] array doesn't contain a definition for limits. HingeJoint is a Struct so I need to over write it which I can do on a single joint at a time (Its even done in the linked Unity documentation in my initial post) but am having issues with in an array. Also testing your for each transform which seems to miss the point of min max limits on joints has an error as you can't convert a method group GetComponent to non-delegate type HingeJoint. I could just be doing this wrong but in testing all I get is a web of errors.
Your answer
