Game Development Reference
In-Depth Information
31.
32.
}
33.
}
34.
}
35.
}
36.}
Listing 19:
A script that detects collisions between an object and the targets in the scene
TargetHitDestroyer
script introduces to us the concept of arrays. An array is a collection
of objects that have the same type, and can be accessed through a single variable. In
line 12 of Listing 19 we declare the array
allTargets
, in which we are going to store all
the targets in the scene. You can easily recognize arrays through the square brackets []
in their declaration. In the same line we call the function
FindObjectsOfType()
and give it
the type
Target
, which is the script added to the target prefab, and hence exists in all target
objects. This function will search the scene for any object that has
Target
attached to it.
FindObjectOfType<Target>()
returns to us an array that contains all targets, and we store
this array in
allTargets
.