Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
5
Question by jeffweber · Sep 02, 2011 at 10:31 AM · getcomponent

How to distinguish between multiple components of same type

Lets say I have a "torso" GameObject and I use 2 hinge joint components to attach a left and right arm.

Now, in script, I want to control each hinge joint separately.

How can I set things up so that in my code I can distinguish between the hinge joint for the left arm and the hinge joint for the right arm.

-Jeff Weber

Comment
Add comment · Show 5
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 mohanrao164 · Sep 02, 2011 at 10:57 AM 0
Share

u can use tags and distinguish between them.

avatar image jeffweber · Sep 02, 2011 at 11:40 AM 1
Share

But I don't see a way to set a component tag in the editor and I don't see a GetComponentByTag method.

avatar image Joshua · Sep 02, 2011 at 12:38 PM 3
Share

@mohanrao164 Tags don't go on components.. read the question.

avatar image Waz · Sep 02, 2011 at 01:45 PM 1
Share

I presume this is purely for example purposes, but note that the "normal" way to structure a skeletal structure would put the joint components on the arms, not the torso, such that each Rigidbody has just one joint, regardless of how many appendages join to it.

avatar image jeffweber · Sep 02, 2011 at 02:22 PM 0
Share

The question was partially for example purposes as I see this co$$anonymous$$g up in other situataions, but it is also specific to something I'm trying to do. I'll try to design my bodies the way you suggest.

5 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Joshua · Sep 02, 2011 at 12:37 PM

You can only distinguish between several instances of the same class by their properties and variables, so in this case the second body they´re connected to.

Comment
Add comment · Show 5 · 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 Bravini · Sep 02, 2011 at 01:29 PM 1
Share

you could also add the hingejoints through scripting and cache them, especially if the connected bodies also have multiple hinges, but in your case joshua's anwer should do it

avatar image Joshua · Sep 02, 2011 at 02:12 PM 0
Share

Yeah, saving a referencing is definitely the normal way to do it. Unity makes it sometimes impossible to do things the normal way though, for instance here - you'd need to know the position and rotation, etc of the joints beforehand.

avatar image jeffweber · Sep 02, 2011 at 02:24 PM 0
Share

$$anonymous$$inda wish the "tag" property on components was exposed in the inspector and there was a GetComponentByTag method. Oh well, I think I can work around it by checking the attached body as Joshua suggests. Or by composing my character differently as suggested above by Warwick. Thanks all.

avatar image Bunny83 · Sep 02, 2011 at 02:36 PM 2
Share

@JeffWeber: Components don't have tags or names. The component class have those properties but they just return the name or tag of the GameObject it's attached to.

avatar image ransomink · Jan 14, 2018 at 07:00 PM 0
Share

Actually, you can distinguish between multiple instances...

avatar image
4

Answer by RyanFaeScotland · Mar 14, 2015 at 01:13 AM

I've got 2 Audio Sources connected to my main camera, one for music and one for the only sfx in my simple game which is called "Blop".

When I create the object that is going to trigger the sound effect I get a reference to the correct Audio Source from the camera thus:

 public class MyClass : MonoBehaviour {
 
     private AudioSource source;
     
     // Use this for initialization    
     void Start () {
         foreach (AudioSource aSource in Camera.main.GetComponents<AudioSource>())
         {
             if(aSource.clip.name.Equals("Blop")){
                 source = aSource;
                 break;
             }
         }
     }

I can't guarantee it's the best way as I am just cutting my teeth with Unity but it is simple and sometimes that's all you need.

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 Bynar1M4n · Jan 14, 2018 at 06:25 PM 0
Share

Nice solution. It could also be:

    void Start () {
     aSource = Camera.main.GetComponents<AudioSource>()[indexFromAudiosource];
      }

But, depend of the index of the Audiosources.

Regards.

avatar image
2

Answer by Bunny83 · Sep 02, 2011 at 02:42 PM

You can also do it the Unity's drag&drop way :D

If you have a script of your character just add two public variables of type HingeJoint. Now you can drag-drop the two joints onto those variables.

I'm not a fan of that way. Under some circumstances Unity can loose those references so i usually aviod too complex inspector-reference-setups. But for such small things it should be no problem.

Comment
Add comment · Show 4 · 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 Bravini · Sep 02, 2011 at 07:19 PM 0
Share

the most common way to lose reference is if the referenced object in the drag & drop (exposed variable) is not in the object's hierarchy and you try instantiating the prefab on another scene.

avatar image Joshua · Sep 02, 2011 at 07:30 PM 0
Share

Yeah, hate doing it that way, but it works I guess... ;)

avatar image Bravini · Sep 02, 2011 at 08:35 PM 0
Share

I like it cause it's great for testing, sometimes I do it this way and on script I put if(!object) GameObject.Find("object"), this way I can cache and keep it simple on short projects, but it's still sloppier than instantiating through scripting

avatar image sarahnorthway · Aug 22, 2016 at 12:09 AM 0
Share

It's tricky if the component controlling the two HingeJoints is on a different GameObject. Then you need to open them in two different Inspector windows (and use the lock icon) to be able to drag between them.

Yes drag&drop is prone to lost references, but can be more easily used by non-coders.

avatar image
0

Answer by Ertug · Jul 13, 2014 at 10:05 AM

You can define variables for a specific type of component that you later wish to refer to. In my case I defined FixedJoint variables:

 var newFixedJoint1 : FixedJoint;
 var newFixedJoint2 : FixedJoint;

Then I used these variables to define FixedJoints that I added to myGameObject:

 newFixedJoint1 = myGameObject.AddComponent(FixedJoint);
 newFixedJoint2 = myGameObject.AddComponent(FixedJoint);

And then later I was able to refer to those FixedJoint components separately like this:

 newFixedJoint1.connectedBody = someGameObject.rigidbody;
 newFixedJoint2.connectedBody = someOtherGameObject.rigidbody;

That's JavaScript but you can use this method as a solution I think.

Comment
Add comment · 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
0

Answer by ransomink · Jan 14, 2018 at 06:58 PM

You can distinguish between multiple components by using GetComponents.

 HingeJoint[] hingeJoints;
 
 void Awake()
 {
     hingeJoints = GetComponents<HingeJoint>();
 }

They will be placed in order from top-to-bottom. If the first hinge joint is the left arm you'll use hingeJoints[0] and hingeJoint[1] for the right arm.


Alternatively, you can reference them for easier access, like so:

 HingeJoint[] hingeJoints;
 HingeJoint leftArm;
 HingeJoint rightArm;
 
 void Awake()
 {
     hingeJoints = GetComponents<HingeJoint>();
     leftArm     = hingeJoint[0];
     rightArm    = hingeJoint[1];
 }
Comment
Add comment · Show 2 · 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 MOTYSHIZ · Mar 16, 2018 at 02:51 AM 0
Share

This should be the top answer, tbh.

avatar image Bunny83 MOTYSHIZ · Mar 16, 2018 at 03:26 AM 0
Share

At the time the question was asked (and the other answers were posted) Unity did not return a particular order in the GetComponents call. The order preserving comes with Unity 5.x i think. However it's still a better approach to assign the components in the inspector to the right slots / variables

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

13 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

Related Questions

Accessing other objects efficiently 1 Answer

Destroy(GetComponent(...)) not working 1 Answer

GetComponent(); in Awake cant be accessed in Update 1 Answer

Issues with 3.3 to 3.4 javascript update 1 Answer

GetComponent returns null reference 0 Answers


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