Asteroids – Parent and Child Objects
In the last part we set up our model ready to use in our game. There is one more thing we are going to do with our model.
In Unity objects can be Parents or Children (or both or neither).
Parent objects have child objects attached to them. If the Parent object moves, so will the children.
The reverse is not true. If a child object moves the parent will not be affected and neither will any of its siblings.
An object can be a child of one object and have its own children in turn. Again if this object moves so will the children but the parent object will not.
So why do we use parent and child objects? Well there are several reasons.
Firstly they help us organise our objects in a scene. Similar objects can be grouped together with an empty object as the parent.
Secondly they allow us to apply a single instance of a script to a parent object to effect all the children at once.
Lastly they allow us a very easy method to swap out models for other ones later in development. For example you may want to prototype the character movement but do not have a character model ready. You can have a parent object called player with all the scripts which will effect the character on it and have a child object which is a primitive shape (like a cube or capsule) to prototype with. Once the model is ready it is a simple case of swapping the primitive shape for the character model without doing much work.
For our game we will be making the ship model a child of our player object. To do this click and drag the model in the hierarchy onto the player object.
This way we can put all our scripts on the player object and keep the graphics separate so we can easily switch our ship model later on if we need/want to.
In the next part we will look at functions/methods (either name can be used).