Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by buxton4life · Jul 21, 2012 at 03:10 PM · javascriptarraylooplevelsfor

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
      }
      }
  }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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..

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image buxton4life · Jul 21, 2012 at 04:28 PM 0
Share

Thanks for the reply but I get two errors my friend:

  1. $$anonymous$$ identifier 'evt'.

  2. Internal compiler error: Array index is out of range..

avatar image buxton4life · Jul 21, 2012 at 04:32 PM 0
Share

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];
      }
      }
  }
  }
avatar image Seth-Bergman · Jul 21, 2012 at 04:41 PM 0
Share

DUH! got it, stupid error, see again, fixed now

I had Application.LoadLevel[i+1];

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges