Game Development Reference
Fig 1.30 Game view of a matrix of cubes created with Listing 1.23 .
Sometimes a single variable is a less efficient way of storing data and
objects. For example, consider changing the color of each of the cubes
created in Listing 1.21 , not initially at the beginning of the program,
but randomly and constantly while it is running. In Listing 1.21 , a single
variable is used to create nine cubes. However, the variable itself only
ever holds one cube at a time. Each time a new cube is created, the
variable is overwritten with a new one. This means that after a new
cube is created and assigned to aP , it is no longer possible to access
the properties of the previous cube. Even the ability to change its
position is gone. Therefore, we need the variable aP to hold not just one
GameObject, but nine.
This can be achieved by making aP into an array as shown in Listing 1.24 .
In Listing 1.24 , the variable aP is no longer a single Game Object but an array.
An array can store anything, including integers, floats, and Game Objects.
If the original aP was a single storage box in memory, this new aP is a row of
storage boxes where each box has its own index number.