I am an amateur game developer, lets get that said first and foremost. As I have been working on my game for a few months now, I am starting to face to some design decisions in regards to how I should structure my game objects in the scene hierarchy. Right now I place them where I think it makes sense:
- I have a empty game object that has children that are the managers and systems.
- I have a terrain with the terrain elements (rocks, trees, grass, etc) as children of the terrain they are part of.
- I have a character spawner that is placed … well, not quite sure yet. Hence the question.
Are there a set of inherent rules when it comes to deciding what should be a parent or child of something else. I figure it mainly matters in regards to position and rotation but I cant think of any other reason why other than simply organizing. I presume if everything was on the same level then the game could still function than if it were well organized with an actual hierarchy.
When should I child an object and why, or why not?
You are right, you should make the objects childs when they want to get affected by the transform of the parent or for organization porpuse using empty gameobjects. there is no rule, so basically you get to decide what better suits for your game
Unity offers a number of helper methods to help navigate the hierarchy. Methods like GetComponentsInChildren<> can be quite useful. Especially when you are spawning lot’s of dynamic objects that are affected by parent transforms. For example, imagine a platform gameobject that moves around. You can easily spawn objects (creatures?) on to the platform (i.e. a child of the platform gameobject) and manipulate their “local” Transform properties (like transform.localPosition, etc). As the platform moves, they move and in turn they move on the platform (via the local manipulations). When you want to get to them in to a platform or other game manager script/behaviour, you can use the GetComponentsInChildren on the platform gameobject or transform.
I must say as well, you sometimes need a hierarchy just to organise things a little. Not ideal this but we are only human!