- Home /
How to correctly shorten this script using arrays and iterations
Hullo elite coders of the universe, I am new to this coding buisness and was wondering how to use for loops and arrays to shorten the script below:
var l1 : GUITexture;
var l2 : GUITexture;
var l3 : GUITexture;
var l4 : GUITexture;
var l5 : GUITexture;
var l6 : GUITexture;
var l7 : GUITexture;
var l8 : GUITexture;
var l9 : GUITexture;
var l10 : GUITexture;
var l11 : GUITexture;
var l12 : GUITexture;
var l13 : GUITexture;
var l14 : GUITexture;
var l15 : GUITexture;
var l16 : GUITexture;
var l17 : GUITexture;
var l18 : GUITexture;
function Update () {
for (var evt : Touch in Input.touches)
{
var l1T = l1.HitTest(Input.mousePosition);
var l2T = l2.HitTest(Input.mousePosition);
var l3T = l3.HitTest(Input.mousePosition);
var l4T = l4.HitTest(Input.mousePosition);
var l5T = l5.HitTest(Input.mousePosition);
var l6T = l6.HitTest(Input.mousePosition);
var l7T = l7.HitTest(Input.mousePosition);
var l8T = l8.HitTest(Input.mousePosition);
var l9T = l9.HitTest(Input.mousePosition);
var l10T = l10.HitTest(Input.mousePosition);
var l11T = l11.HitTest(Input.mousePosition);
var l12T = l12.HitTest(Input.mousePosition);
var l13T = l13.HitTest(Input.mousePosition);
var l14T = l14.HitTest(Input.mousePosition);
var l15T = l15.HitTest(Input.mousePosition);
var l16T = l16.HitTest(Input.mousePosition);
var l17T = l17.HitTest(Input.mousePosition);
var l18T = l18.HitTest(Input.mousePosition);
if (evt.phase == TouchPhase.Stationary)
{
if(l1T)
{
Application.LoadLevel(1);
}
if(l2T)
{
Application.LoadLevel(2);
}
if(l3T)
{
and so on.....
}
}
}
HERE IS MY FEEBLE ATTEMPT ------------------->
var levels : int[];
levels = new float[15];
for(var i = 0; i < 14; i++) { if([i] == ) {
DO SHIT! }
Thats as far as I got :( ANy help would be rewarded with endless love and appreciation.
Answer by buxton4life · Jul 21, 2012 at 04:44 PM
A few tweaks on the above answer works perfectly! Thanks bro.
var myTextures : GUITexture[] = new GUITexture[18];
function Update(){
for(var i=0;i<myTextures.length;i++)
{
if(myTextures[i].HitTest(Input.mousePosition))
{
for (var evt : Touch in Input.touches)
{
if (evt.phase == TouchPhase.Stationary)
Application.LoadLevel(i+1);
}
}
}
}
Answer by Seth-Bergman · Jul 21, 2012 at 03:48 PM
var myTextures GUITexture[] = new GUITexture[18]; // you could set these in the inspector.
function Update(){
for(var i=0;i<myTextures.length;i++)
{
if(myTextures[i].HitTest(Input.mousePosition))
{
for (var evt : Touch in Input.touches)
{
if (evt.phase == TouchPhase.Stationary)
Application.LoadLevel(i+1);
}
}
}
}
think that should work.. of course don't forget to assign the GUITextures to the array..
Thanks for the reply but I get two errors my friend:
$$anonymous$$ identifier 'evt'.
Internal compiler error: Array index is out of range..
Fixed the 1st error! Forgot to add the other for loop before the if statement:
var myTextures : GUITexture[] = new GUITexture[18];
function Update(){
for(var i=0;i<myTextures.length;i++)
{
if(myTextures[i].HitTest(Input.mousePosition))
{
for (var evt : Touch in Input.touches)
{
if (evt.phase == TouchPhase.Stationary)
Application.LoadLevel[i+1];
}
}
}
}
DUH! got it, stupid error, see again, fixed now
I had Application.LoadLevel[i+1];
Your answer
Follow this Question
Related Questions
How to loop a function? 2 Answers
How do I reset a for loop variable??? 3 Answers
For Loop Array Problem 1 Answer
how do i make a loop for a boolean? 2 Answers
Adding an object to an array only if it is not in the array 2 Answers