Time for action – finding data and breakout of the while loop
We're going to do something a little different in this loop. Once we find the pony we want, we'll breakout of the while loop. This is handy when looping through a large list of objects. When the desired data is found, there's no sense in continuing to loop through the rest of the list:
- Modify
LearningScript
as shown in the next screenshot. - Save the file.
- In Unity, click on Play.
What just happened?
If we have been searching for Fluttershy
instead of Rainbow Dash
, and not included the break
keyword on line 19, the output would have been exactly the same as the for
loop example. In fact, the ...
Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.