unity 3d student

Beginner B24 – For Loops

How to use a For Loop in Unity javascript to repeat a set of instructions until a condition is met.

Code Used (Javascript)

var myPrefab : Rigidbody;
  1. var distanceMultiplier : float = 2;
  2.  
  3. function Start(){
  4.  
  5.     var i : int = 0;
  6.     var pos : Vector3 = transform.position;
  7.  
  8.     for(i=0; i<=3; i++){
  9.         Instantiate(myPrefab, Vector3(pos.x+i*distanceMultiplier, pos.y, pos.z), transform.rotation);
  10.         yield WaitForSeconds(0.5);
  11.         Debug.Log("made ball "+i);
  12.  
  13.     }
  14.  
  15. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

17 Comments

    […] More Info: Beginner B24 – For Loops […]

  • Wow. Very clear and informative. Thanks so much!

  • Scene entitled forLoopz. You’re so gangster.

    Great tutorials, simple and easy to digest and in plain English. Yours are the best I’ve seen so far, keep it up!

    • Cheers Craig, fer blates blud I is gangster. etc.

      • LOL, OMG!! Sorry, that reply was priceless. Thank you for these tutorials they are a great start, Though I’m not done yet, I have a question. What would be a good next stepping stone after the tutorials? I am doing a few small projects with them though. Thanks again.

    The “Next” button advances to C01, and the “Previous” button C01 goes back to B22. Thought you ought to know 🙂

    Heck, to make updating simpler you could write a JavaScript that grabs the next one in the list, and changes the link accordingly. Or you could just change it manually 😀

    Pe-ads

    • Cheers – yeah tis set manually, I just forgot, ta!

    can anyone tell my what a “expression cannot be assigned to error is, or why this script would throw one?

    function Update () {
    var i : int = 0;
    for(i=0; i<=1; 1++){
    guiText.text = "Time"+ i;
    }
    }

    • Yes you can’t create an expression that way – your for loop should say i++ at the end, not 1++. Just an accidental oversight!

    ok so i thought this would have been a super easy script to write, but it seems i was wrong, i’m trying to have an “ammo” collide with an object twice and on the second hit destroy it. (a 2 hit kill) I am trying to use a for loop todo this but if you know another way it would be great!

    var star : ParticleEmitter;
    //var En : Rigidbody;

    function OnCollisionEnter(theCollision : Collision){
    if(theCollision.gameObject.name == “bullet(Clone)”){
    Instantiate(star, transform.position, transform.rotation);
    Destroy(gameObject);
    if(theCollision.gameObject.name == “bull(Clone)”){
    var i : int = 0;
    for(i = 0;i>= 2;i++)
    if (i == 2)
    {
    Destroy(gameObject);
    }

    }
    }
    }

  • Thank you very much for these tutorials.. they are helping me a lot.

    I would like to ask if it is possible to modify in the same fashion you did the transofrm.rotation when instantiating an object in a for cycle?

    I think you can do that but how is the script?

    Thank you very mauch again.

  • Ok so here it is on c#…

    using UnityEngine;
    using System.Collections;

    public class ForLoop : MonoBehaviour {

    public GameObject myPrefab;
    public float DistanceMuliplier = 2;

    // Use this for initialization
    void Start ()
    {
    StartCoroutine( MakeBox() );
    }

    IEnumerator MakeBox()
    {

    for (int i = 0; i <=3; i++)
    {
    Vector3 pos = transform.position;
    Instantiate(myPrefab, new Vector3(pos.x, pos.y, pos.z + i * DistanceMuliplier), transform.rotation);
    yield return new WaitForSeconds(0.5f);
    }

    }
    }

  • As a test I created a for loop that iterated through numbers using i as the index number of the objects I was referencing, cutting the 20 lines of code in my waypoint mapping script to 3 total… I’ll post my concept test for it here, so you can see my sample code. After that I just made objects for each number (box1base, box2base, etc…) and it cycled through it all for me :D. This one just locates the boxes by number and alters their colors (with a 1 second delay to ensure you can see it working well).

    I plan on using it to create numbered instances of my waypoints for my enemy to travel in a tower defense game. Here’s the code in Unityscript I made in a file named “testForLoops”:

    function Start ()

    {

    for (i = 0; i < 10; i++)

    {

    var tempName : GameObject;

    tempName = gameObject.Find("box"+i+"base");

    Debug.Log("referencing to "+tempName);

    tempName.transform.renderer.material.color = Color.blue;

    yield WaitForSeconds(1);

    } //for i

    } // Update

  • i think the code used section on this page has a small typo. Instead of:

    for(i=0; i<=3; i++){

    It should be

    for(i=0; i<=3; i++){

    • i mean in the previous one

      instead of “<" it was coming &lt

    Very good tutorials Will, I bought your book and is good, the coding are simple and best of all it covered both C# and Javascript.

    Any good video tutorials or books you can recommend for Unity3D iOS devices?

  • var myPrefab : Rigidbody;

    Or

    Var myPrefab : GameObject;

    (Sorry my English is very bad)

Leave a Comment