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 Jason Hamilton · Apr 09, 2011 at 12:01 AM · javascriptarrayplayerprefshighscores

Sorting arrays?

Hey, heres my script;

function OnGUI ()
{
    var arr = new Array();
    arr.length = 5;
    arr[0] = PlayerPrefs.GetString("Score1");
    arr[1] = PlayerPrefs.GetString("Score2");
    arr[2] = PlayerPrefs.GetString("Score3");
    arr[3] = PlayerPrefs.GetString("Score4");
    arr[4] = PlayerPrefs.GetString("Score5");
    arr[5] = PlayerPrefs.GetString("CurrentScore");
    arr.Sort();
    GUI.Label (Rect (10,30,150,100), arr[0]);
    GUI.Label (Rect (10,50,150,100), arr[1]);
    GUI.Label (Rect (10,70,150,100), arr[2]);
    GUI.Label (Rect (10,90,150,100), arr[3]);
    GUI.Label (Rect (10,110,150,100), arr[4]);
}

it should get those values and add the current score, then show the top 5 (add the current score to the board if it is high enough), one problem, it doesn't work. :(

Comment
Add comment · Show 2
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 Eric5h5 · Apr 09, 2011 at 12:22 AM 0
Share

Why are you doing that stuff with arrays and PlayerPrefs every frame in OnGUI? That's terrible for performance, and completely unnecessary; just do it once. Also, using Array is rarely a good idea. Built-in arrays and Lists are much better. See here for using arrays with PlayerPrefs: http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs

avatar image Jason Hamilton · Apr 09, 2011 at 01:53 AM 0
Share

know of a tutorial/forum thread that explains the for statement? I don't get the whole i thing... :P

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 09, 2011 at 02:56 AM

Just a few hints:

  • you set the array length to 5 but you try to set the 6th element (index 5).
  • the highest index is always the element count minus 1. Length 5 means there's space for 5 elements but you start counting at 0 so you end at 4.
  • You used GetString (probably SetString when you saved them) to get the scores. But strings are not numbers and therefore they aren't compared the same way.
  • Like Eric said the Array class is quite bad. It's slow due to dynamically typed elements and can cause problems if you don't cast them to the right type.

"for" statements are quite easy, here's a small explanation:

for (part1; part2; part3)
{
    //loop body
}

All the code within the brackets belong to the loop. If you declare a variable in part 1 it's not available outside of the loop

  1. part1 is executed right before the loop starts. It's used to initialize the loop.
  2. part2 is executed right before each iteration step. This part evaluates to a boolean. If the expression in part 2 returns false the loop is aborted. As long as this evaluates to true the body is executed and the loop runs on.
  3. part3 is executed after the loop body have been executed. This is used to prepare the next iteration step.

A typical loop works with an integer variable that counts up or down:

for (i=0; i<5; i++)

This loop will run 5 times (0, 1, 2, 3, 4). After the 5th iteration (i == 4) the 3th part will increase i again to 5 but when part 2 is evaluating the expression (5<5) it aborts here.

You are not restricted to use an integer variable. Here you can iterate through a transform's hierarchy upwards till you reach the top (the overall parent doesn't have a parent)

for (var T : Transform = transform; T != null; T = T.parent)


The second version is the for (each) loop to iterate through an enumerable object.

for(variable in enumerable)

As example: the Transform class is an enumerable type. You can iterate through all childs of this transform:

for (var T : Transform in transform)

within the for body the variable T always holds one element of the enumerable object.


There are some special "commands" that can be used inside a loop:

  • break; break exits the loop at the current position and continue with the code after the loop-body.
  • continue; continue breaks also the current iteration but it doesn't exit the loop. It just go on with the next iteration. So it just skips the rest of the loop body.
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

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

No one has followed this question yet.

Related Questions

Is there a decent tutorial for a local high score table (android)? 1 Answer

HighScore Manage Using PlayerPrefs 1 Answer

PlayerPrefs Not Working 0 Answers

How can I store highscores in PlayerPrefs? 3 Answers

PlayerPrefs not saving my variable value for HighScore System 3 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