Component vs Child GameObject

In the Project Setup video for the Laser Defender module, Gary presents a challenge to create Player and Enemy prefabs. When I did this myself, I created empty GameObjects for each, and then added a SpriteRenderer component to each for the player/enemy image. When Gary does this, instead of adding a SpriteRenderer component to the parent object, he creates a child object for the sprite image.

Is there reason to do it one way vs the other? I can see the value in having the inspector less cluttered with multiple components, but also wondered if there was extra overhead for the additional transforms of the child objects. Just wanted to understand the reasoning behind the decision, if there was any.

Thanks!

Great question!
The main reason I do it this way is really just for convenience - It’s a lot easier to work with the graphical elements of your game objects if they’re separated into a child object.

For example, when you get to the “Enemy Shooting” lecture, you’ll find that the enemy exhibits some weird behaviour and we need to rotate our sprite to help fix it. These kinds of issues are much easier to resolve when you only need to worry about the child object. If your sprite is stored on the parent object then you need to start adding exceptions to your code, which can get pretty messy.

This sort of problem is even worse when you move into 3D games, since there are almost always going to be rotation and scaling issues with the models you bring in.

The other benefit to doing things this way is that it makes swapping out your graphical elements really simple - just rip out the existing child and drop in a new one.

I hope that helps explain my logic. There’s certainly no harm in keeping everything on the parent. I just find that my way of structuring things tends to make life easier in the long run :slight_smile:

1 Like

That makes a lot of sense, thanks for the explanation!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms