Time for action – create a List of pony names
Create a List
that stores the names of some ponies. Since they are names, use the string
type.
Modify
LearningScript
as shown in the next screenshot.Notice the change on line 2.
Save the file.
In Unity, click on Play.
What just happened?
The following screenshot is the Console output. Notice the first output tells you there is a total of 3 elements in the List
:
Note
Please notice that your code is using dot syntax, which will be discussed in more detail in the next chapter. The main concepts I want you to focus on here are the features of a List
.
The analysis of code is as follows:
The code on line 2 is as follows:
Using System.Collections.Generic;
To be able to use a
List
, this tells Unity where to find the necessary C# code files for using aList
.Change the using statement to
using System.Collections.Generic;
.The code on line 8 is as fololws:
List<string> myFavoritePonies = new List<string>();
This statement creates an empty
L
ist
object.First...