4

If I have a GameObject with multiple Component classes attached to it of the same type (let's call this type HingeJoint for example purposes), which HingeJoint component will I get if I called the function GetComponent<HingeJoint>() on my GameObject?

According to my research, this answer claims Unity will simply return the first match from the array of Components--however, the answer is only an educated guess based on the answerer's own game engine design that strives to mimic Unity and not an authoritative source for what Unity actually does.

Jimmy Huch
  • 4,400
  • 7
  • 29
  • 34
  • Perhaps it's not documented since if you have a *specific* requirement you can `GetComponents` and select the one you want. Leaving `GetComponent` free to return an arbitrary one that they're not "contracturally" obligated to guarantee in future versions. – Damien_The_Unbeliever Jan 31 '18 at 09:23

2 Answers2

5

Every object and it's subobjects in Unity are hierarchically placed in a collection. You can call it a "WYSIWYG" because as you see every object in the scene hierarchy is loaded in the same order they are displayed on that list. The same thing applies to the components and it's the main reason why Transform component is placed on top of every other component.

GetComponent will return the first item from the collection of Components seen from the top of the list. To be exactly sure which component will be returned you can order them as you wish in the inspector view.


From the documentation page:

The order you give to components in the Inspector window is the order you need to use when querying components in your user scripts. If you query the components programmatically, you’ll get the order you see in the Inspector.

mrogal.ski
  • 5,828
  • 1
  • 21
  • 30
5

EDITED after m.rogalski correction

The manual indicates that components are checked in the order you put them in the inspector, so GetComponent() will give you the first one of type T

How ever I would recommend not to use inspector ordering, but instead use

GetComponents<T>()

To retrieve them all and dynamically choose the one you need (See the doc if needed)

This is because it would make it really difficult for you to maintain your project if the order of your components mattered. Any other person working on the project would need to know the convention in which these components must be placed. Even if you work alone, you would need to be very careful when adding or removing components, and in case of a bug, your IDE won't be able to give you a clear error

Basile Perrenoud
  • 4,039
  • 3
  • 29
  • 52
  • There is a guarantee on which component will be returned, please read the documentation first. – mrogal.ski Jan 31 '18 at 09:42
  • Well no need to be condescending. , the doc for GetComponent says nothing about that. But you are right, I'll edit my answer about the order. I maintain that it would be bad practice to use this convention tough – Basile Perrenoud Jan 31 '18 at 09:57
  • It's not even bad practice, it's more convenient to loop through desired types of components, you can then spot the ones you need instead of relying on the collection's order. – mrogal.ski Jan 31 '18 at 10:01
  • I think that is what I say. Loop through desired components is fine. But I think that the logic of which are desired should be in code – Basile Perrenoud Jan 31 '18 at 10:04