Game Development Reference
In-Depth Information
More information on the for loop and its usage in C# can be found at
http://msdn.microsoft.com/en-gb/library/ch45axte.aspx .
The while loop
Both the for and foreach loops were especially useful when cycling through an array,
performing specific operations on each iteration. The while loop, in contrast, is useful
to continually repeat a specific behavior until a specified condition evaluates to false .
For example, if you must deal damage to the player as long as they're standing on
hot lava or continually move a vehicle until the breaks are applied, then a while
loop could be just what you need, as shown in the following code sample 1-7:
01 using UnityEngine;
02 using System.Collections;
03
04 public class MyScriptFile : MonoBehaviour
05 {
06 // Use this for initialization
07 void Start ()
08 {
09 //Will count how many messages have been printed
10 int NumberOfMessages = 0;
11
12 //Loop until 5 messages have been printed to the console
13 while(NumberOfMessages < 5)
14 {
15 //Print message
16 Debug.Log ("This is Message: " +
NumberOfMessages.ToString());
17
18 //Increment counter
19 ++NumberOfMessages;
20 }
21 }
22
23 // Update is called once per frame
24 void Update ()
25 {
26 }
27 }
 
Search WWH ::




Custom Search