• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Giantbean · Apr 08 at 06:35 PM · hingejoint

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;
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
       }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Giantbean · Apr 09 at 02:20 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

136 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pick up a chain of objects 0 Answers

Connectors with unintended effect of spring before breaking up 0 Answers

Visual Way to Set Anchor Points in Hinge Joint 2D 2 Answers

Can't change HingeJoint properties by script 1 Answer

Hinge to a child, Parent has rigidbody 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges