Beginner's Scripting Guide

From Unify Community Wiki
Revision as of 20:52, 10 January 2012 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):

function Update() 
{
	for (var i=0; i < someNumber; i++)
	{
		something something;
	}
}

The for loop can also be used to iterate of the contents of collections (ie. lists and arrays)

function ToggleRenderers() 
{
	var aList = GetComponentsInChildren(Renderer);
	for ( var r in aList )
	{
		r.enabled= ! r.enabled;
	}
}

The while loop

A 'while' loop in a co-routine:

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;
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox