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 · Sep 13, 2012 at 11:51 AM · javascriptarrayforloopsscores

For Loop Array Problem

Good morrow, good morrow,

I have this piece of code looping through the star scores and then producing either a gold, silver or bronze for the amount of stars collected for each level. The code seems only to work on the last item in the array. So all of the levels have the value of the 54th array item. In this case every level echo's out a gold trophy. This output is controlled by another bit of code which prints an array of the level, score and trophy.

 trophy = new String[54];
     starScores = new float[54];
     
     for(var n : int = 0; n<starScores.length; n++){
       
        starScores[n] = PlayerPrefs.GetFloat("s"+(n+1));
       
       for(var j : int = 0; j<54; j++){
         
       
         if(starScores[n] >= 5 && starScores[n] < 10){
             trophy[j] = "gold";
         } if(starScores[n] == 3 && starScores[n] < 5){
             trophy[j] = "silver";
         } if(starScores[n] == 0 && starScores[n] < 3){
             trophy[j] = "bronze";
         }
         
         }
         }    
      totalStarScore = starScores[0] + starScores[1] + starScores[2] + starScores[3] + starScores[4] + starScores[5] + starScores[6] + starScores[7] + starScores[8] + starScores[9] + starScores[10] + starScores[11] + starScores[12] + starScores[13] + starScores[14] + starScores[15] + starScores[16] + starScores[17];    
     array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
               trophy[4],trophy[5],trophy[6],trophy[7],
               trophy[8],trophy[9],trophy[10],trophy[11],
               trophy[12],trophy[13],trophy[14],trophy[15],
               trophy[16],trophy[17],trophy[18],trophy[19],
               trophy[20],trophy[21],trophy[22],trophy[23],
               trophy[24],trophy[25],trophy[26],trophy[27],
               trophy[28],trophy[29],trophy[30],trophy[31],
               trophy[32],trophy[33],trophy[34],trophy[35],
               trophy[36],trophy[37],trophy[38],trophy[39],
               trophy[40],trophy[41],trophy[42],trophy[43],
               trophy[44],trophy[45],trophy[46],trophy[47],
               trophy[48],trophy[49],trophy[50],trophy[51],
               trophy[52],trophy[53]];

Any idea of the problem within this code i'm guessing its got something to do with the two for loops! Any help would be much appreciated. Thank You.

Comment
Add comment · Show 1
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 AlucardJay · Sep 13, 2012 at 01:11 PM 2
Share
 array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
        trophy[4],trophy[5],trophy[6],trophy[7],
        trophy[8],trophy[9],trophy[10],trophy[11],
        trophy[12],trophy[13],trophy[14],trophy[15],
        trophy[16],trophy[17],trophy[18],trophy[19],
        trophy[20],trophy[21],trophy[22],trophy[23],
        trophy[24],trophy[25],trophy[26],trophy[27],
        trophy[28],trophy[29],trophy[30],trophy[31],
        trophy[32],trophy[33],trophy[34],trophy[35],
        trophy[36],trophy[37],trophy[38],trophy[39],
        trophy[40],trophy[41],trophy[42],trophy[43],
        trophy[44],trophy[45],trophy[46],trophy[47],
        trophy[48],trophy[49],trophy[50],trophy[51],
        trophy[52],trophy[53]];

This could be cleaned up with a loop :

  for (var l:int = 0; l < 54; l ++)
  {
       array4[l] = trophy[l];
  }

or even just simply :

 array4 = new String[54];
 array4 = trophy;

1 Reply

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

Answer by hirenkacha · Sep 13, 2012 at 12:01 PM

 trophy = new String[54];
     starScores = new float[54];
 
     for(var n : int = 0; n<starScores.length; n++){
 
        starScores[n] = PlayerPrefs.GetFloat("s"+(n+1));
 
      
        if(starScores[n] >= 5 && starScores[n] < 10){
          trophy[n] = "gold";
        } if(starScores[n] >= 3 && starScores[n] < 5){
          trophy[n] = "silver";
        } if(starScores[n] >= 0 && starScores[n] < 3){
          trophy[n] = "bronze";
        }
 
        
        } 
     totalStarScore = starScores[0] + starScores[1] + starScores[2] + starScores[3] + starScores[4] + starScores[5] + starScores[6] + starScores[7] + starScores[8] + starScores[9] + starScores[10] + starScores[11] + starScores[12] + starScores[13] + starScores[14] + starScores[15] + starScores[16] + starScores[17];   
     array4 = [trophy[0],trophy[1],trophy[2],trophy[3],
            trophy[4],trophy[5],trophy[6],trophy[7],
            trophy[8],trophy[9],trophy[10],trophy[11],
            trophy[12],trophy[13],trophy[14],trophy[15],
            trophy[16],trophy[17],trophy[18],trophy[19],
            trophy[20],trophy[21],trophy[22],trophy[23],
            trophy[24],trophy[25],trophy[26],trophy[27],
            trophy[28],trophy[29],trophy[30],trophy[31],
            trophy[32],trophy[33],trophy[34],trophy[35],
            trophy[36],trophy[37],trophy[38],trophy[39],
            trophy[40],trophy[41],trophy[42],trophy[43],
            trophy[44],trophy[45],trophy[46],trophy[47],
            trophy[48],trophy[49],trophy[50],trophy[51],
            trophy[52],trophy[53]];

I think this should work for you. No need of second for loop. Check rectified if conditions for silver and bronze also. Try and let me know if working fine.. thanks.

Comment
Add comment · Show 2 · 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 · Sep 13, 2012 at 04:10 PM 1
Share

Superb Thankyou! I swear I tried that! But yes the answer was simple in the end! Thankyou @alucardj also your input shorten my code big time!

avatar image hirenkacha · Sep 14, 2012 at 04:13 AM 0
Share

Thats fine @buxton4life.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to correctly shorten this script using arrays and iterations 2 Answers

For all elements above a certain index 1 Answer

Need help using GUI.Button/ input storage 1 Answer

Convert Array into One String (Js) 1 Answer

Help me Convert JS to C# 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