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;
-
var distanceMultiplier : float = 2;
-
-
function Start(){
-
-
var i : int = 0;
-
var pos : Vector3 = transform.position;
-
-
for(i=0; i<=3; i++){
-
Instantiate(myPrefab, Vector3(pos.x+i*distanceMultiplier, pos.y, pos.z), transform.rotation);
-
yield WaitForSeconds(0.5);
-
Debug.Log("made ball "+i);
-
-
}
-
-
}
Further Reading
Related Modules
12 Comments
Leave a Comment









[...] 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);
}
}
}