Storing data in an array, a List, or a Dictionary
There are times that many items need to be stored in some type of list. Perhaps a selection of weapons that a character may use. An example used later in this book is a list of splashscreens for the State Machine project we will build.
There are basically two ways to access items in a list:
Direct retrieval: The location of an item in the list is already known, so code is written to access it directly, or
Loop retrieval: The location of an item in the list is not known, it's just in there somewhere, so code is written to loop through the list until the item desired is found.
First though, we need a list of items before we can select anything from the list. An example of collecting items into a list, then looping through the list, is shown in the Scripting Reference
under the GetComponen
ts()
method:
public HingeJoint[] hingeJoints; void Example() { hingeJoints = gameObject.GetComponents<HingeJoint>(); ... }
All the HingeJoints
in...