Hi Noob GO / Component Questions

Hi all,

I’m new to Unity or will be as soon as I can find a good deal on a Mac. In the mean time, I’m trying to get my head wrapped around the Unity component system and how I can adapt my game design to it. Since I can’t just open the editor and play with it (yet), I was wondering if someone would help explain a few things to me. Basically I’m trying to figure out the relationship between Game Objects, their Components, Child Game Objects, etc.

My questions follow - thanks in advance for your replies.

SC

  1. I understand that Game Objects have components but it looks like components also have components (they have a list of component properties as well as all the GetComponent methods just like a Game Object does). Do these properties methods (of a component) refer to the other components in the same GO or do they refer to components that belong only to that particular component?

  2. I’m a bit confused about how a Game Object can have a property like .HingeJoint that returns a single Hinge Joint component and also contain a .GetComponents that can return an array of Hinge Joints for the same Game Object. Can a Game Object have multiple components of the same type then? If so, is that true for all component types - even scripts/mono behaviors? If a GO contains, multiple HingeJoints, what does .HingeJoint return - the last one added? If not, where do the other components come from?

  1. You’re looking at convenience properties. Components don’t have components. Calling myComponent.GetComponent is the same as calling myComponent.gameObject.GetComponent.

  2. You can have more than one of (some) components. You can add more than one of your custom scripts, but some builtin components, such as a camera, are limited to one per object.

Great - that’s most of what I needed to know.

One last thing - if you add a component that requires another component to a GO that contains more than one component of the required type, how does the newly added component know which one to use?

Thanks again,

SC

A good example would be, to get all Mono scripts attached to an object, you can use GetComponents(MonoBehaviour). That said, I’ve never used GetComponents; of the GetComponent family (GetComponent, GetComponents, GetComponentInChildren, and GetComponentsInChildren), it’s actually the only one I don’t use.

I don’t know for sure, but I think that GetComponent will return “the first one it finds” if there are more than one attached to the gameObject.

It always takes the first one.

Having multiple components of the same type is quite rare in practice.

I think that’s what was confusing me - it seems like you would never need 2 components of the same type in the same GO.

Does anyone have an example of a situation where it was necessary to have multiple components of the same type in the same GO?

Thanks again for all the replies - they’re very helpful.

SC

Just stumbled accross an example:

In the example Networking project, a single player prefab has 2 NetworkView Components each referencing a different script that handles synchronization of that object over a network.

Kind of obscure, but I thought I’d mention it.

Cheers!