Project
Beginner Gameplay Scripting
Loops
Tutorial
Beginner
5 Mins
Unity Technologies
Overview
You have completed 0% of this Tutorial.
1.
Loops
Summary
How to use the For, While, Do-While, and For Each Loops to repeat actions in code.
Language
English
Recommended Unity Versions
4
1.
Loops

ForLoop

using UnityEngine; using System.Collections; public class ForLoop : MonoBehaviour { int numEnemies = 3; void Start () { for(int i = 0; i < numEnemies; i++) { Debug.Log("Creating enemy number: " + i); } } }

WhileLoop

using UnityEngine; using System.Collections; public class WhileLoop : MonoBehaviour { int cupsInTheSink = 4; void Start () { while(cupsInTheSink > 0) { Debug.Log ("I've washed a cup!"); cupsInTheSink--; } } }

DoWhileLoop

using UnityEngine; using System.Collections; public class DoWhileLoop : MonoBehaviour { void Start() { bool shouldContinue = false; do { print ("Hello World"); }while(shouldContinue == true); } }

ForeachLoop

using UnityEngine; using System.Collections; public class ForeachLoop : MonoBehaviour { void Start () { string[] strings = new string[3]; strings[0] = "First string"; strings[1] = "Second string"; strings[2] = "Third string"; foreach(string item in strings) { print (item); } } }

Woohoo! You nailed this tutorial.
Continue rocking your Beginner Gameplay Scripting project.
Next Step:
Scope and Access Modifiers
Tutorial
Beginner
10 Mins
Unity Technologies