Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning C# by Developing Games with Unity 3D Beginner's Guide

You're reading from   Learning C# by Developing Games with Unity 3D Beginner's Guide The beauty of this book is that it assumes absolutely no knowledge of coding at all. Starting from very first principles it will end up giving you an excellent grounding in the writing of C# code and scripts.

Arrow left icon
Product type Paperback
Published in Sep 2013
Publisher Packt
ISBN-13 9781849696586
Length 292 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Terry Norton Terry Norton
Author Profile Icon Terry Norton
Terry Norton
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Discovering Your Hidden Scripting Skills FREE CHAPTER 2. Introducing the Building Blocks for Unity Scripts 3. Getting into the Details of Variables 4. Getting into the Details of Methods 5. Making Decisions in Code 6. Using Dot Syntax for Object Communication 7. Creating the Gameplay is Just a Part of the Game 8. Developing the State Machine 9. Start Building a Game and Get the Basic Structure Running 10. Moving Around, Collisions, and Keeping Score 11. Summarizing Your New Coding Skills A. Initial State Machine files B. Completed code files for Chapters 9 and 10 C. Pop Quiz Answers Index

Time for action – using foreach loops to retrieve data


We're going to create an array, a list and a dictionary, then loop through each one to retrieve the desired data from each one by using foreach loops.

  1. Modify LearningScript as shown in the next screenshot.

  2. Save the file.

  3. In Unity, click on Play.

What just happened?

As we looped through each list, we decided which data to display to the Console:

The analysis of the code is as follows:

For each list we created, we populated them using a Collection Initializer.

  • The code between lines 9 and 10 with its description:

    string[] ponyArray = new string[] {"AppleJack", "Rarity"};

    A string array named ponyArray is declared and two strings are added.

  • The code on line 12 with its description is as follows:

    foreach(string pony in ponyArray)

    A foreach loop is used to retrieve one element, a pony name string, stored in ponyAr ray.

    A variable is declared named pony to hold the retrieved pony name.

    Once a pony name is retrieved, the foreach code block, lines 13 to...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime