Beginner's Scripting Guide

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
Line 13: Line 13:
 
something something;
 
something something;
 
}
 
}
}</javascript>
+
}</syntaxhighlight>
  
 
The for loop can also be used to iterate of the contents of collections (ie. lists and arrays)
 
The for loop can also be used to iterate of the contents of collections (ie. lists and arrays)
Line 23: Line 23:
 
r.enabled= ! r.enabled;
 
r.enabled= ! r.enabled;
 
}
 
}
}</javascript>
+
}</syntaxhighlight>
  
 
=== The while loop ===
 
=== The while loop ===
Line 48: Line 48:
 
yield;
 
yield;
 
}
 
}
}</javascript>
+
}</syntaxhighlight>

Revision as of 20:52, 10 January 2012

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