Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Overview
  • For Loops

Was this helpful?

  1. Coding Basics

Loops

Overview

Loops are great for repeating the same command or comparison for multiple items, so they are often used with arrays.

A loop tells the program to run a piece of code multiple times. The number of times is set based on the conditions you set.

The most common loops in Unity are For Loops.

For Loops

For loops basically say, “run this code for this many times.”

The three elements found in for loops are:

  1. The starting point or initialization value

  2. Conditions to be met

  3. How to increase/decrease the value (steps)

The value change in steps allows you to skip elements in your array (prints every fifth element, etc.).

Format:

for (int i = 0; i < 10; i++)
{
    // Do something
}

In the code above:

int i = 0 is the starting point or initialization value. in this example, we start with zero - a common starting value since arrays start with zero. i is generally used, standing for "index."

i < 10 is the condition to be met to run the code in the brackets. Here, the code will run 10 times, since, on the 11th time, i will be 10 and be false.

i++ is how much to increase the i or index value. ++ is a shorthand for "add 1" and could have also been written as i += 1 (another shorthand) or i = i + 1. This number can also be decreased. The shorthand for that would be i-- or i -= 1; The number it increases or decreases can be any integer.

For Loops are great for updating arrays and lists. Just use i (or your index value) for your index:

public Transform[] cubes;

// Moving all cubes in the cubes array forward
for (int i = 0; i < cubes.Length; i++)
{
     cubes[i].Translate(Vector3.forward);
}

For more information about Arrays and Lists, go to the link below:

PreviousArraysNextFunctions

Last updated 4 years ago

Was this helpful?

Arrays