Beginner's Scripting Guide

From Unify Community Wiki
Revision as of 13:59, 8 January 2011 by NCarter (Talk | contribs)

Jump to: navigation, search
This article is a stub.

You can help UnifyWiki by expanding it.

Help, I need a Loop Guru!

If you find you want to loop (i.e. for your co-routines) but are not sure about what type of loop, you've come to the right place! Here are a list of useful example loops;

The for loop

Basic "for" loop; start value, end value, update expression (operator i increases on each update): <javascript>function Update() { for (var i=0; i < someNumber; i++) { something something; } }</javascript>

The for loop can also be used to iterate of the contents of collections (ie. lists and arrays) <javascript>function ToggleRenderers() { var aList = GetComponentsInChildren(Renderer); for ( var r in aList ) { r.enabled= ! r.enabled; } }</javascript>

The while loop

A 'while' loop in a co-routine: <javascript>var target : Transform;

if (target) { StartCoroutine("Lerp", 1.0); }

function Lerp (time : float) { var pos = transform.position; var rot = transform.rotation; var originalTime = time;

while (time > 0.0) { time -= Time.deltaTime; transform.position = Vector3.Lerp(target.position, pos, time / originalTime); transform.rotation = Quaternion.Slerp(target.rotation, rot, time / originalTime); yield; } }</javascript>

Personal tools