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
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?
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?
You’re looking at convenience properties. Components don’t have components. Calling myComponent.GetComponent is the same as calling myComponent.gameObject.GetComponent.
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.
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?
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.
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.