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

HACKED BY SudoX — HACK A NICE DAY.

Share via Social Media

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

17 Comments