• 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
Question by skwunk · Aug 19, 2015 at 02:17 AM · arrayplayerprefsx

Trying to get an array to show up in Debug.log

I'm trying to script a $$anonymous$$gh score table that could be saved in PlayerPrefsX for my game project, but I can't seem to get the int array that I created for it to even show up in Debug.Log (or anywhere else really) even after creating a test value to see if anyt$$anonymous$$ng is even being entered. Not even the Inspector shows any indication that anyt$$anonymous$$ng happened, all of the values show up as 0. Here's the chunk of code I have for it so far:

 var scoreArray = new int[8];
 var testScore:int = 1000;
 var i:int;
 var j:int;
 
 function Start () {
 
 }
 
 function Update () {
     
     for (i = 0; i > 8; i++) //$$anonymous$$gh score check
         {
             if (testScore > scoreArray[i])
             {                        
                 for (j = scoreArray.length - 1; j <= i; --j ) //pushes the score values down starting from the slot that testScore ends up
                 {
                    scoreArray[j] = scoreArray[j-1];
                 }
                 
                 scoreArray[i] = testScore;
             }
             
             Debug.Log(scoreArray[i]);
         }
 
 }

I prefer solutions in JS as that is the language that I have the most experience with.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Bakshish-Singh · Aug 19, 2015 at 05:01 AM

I t$$anonymous$$nk there is some problem with your code you posted. The for loop on line 12 should be

 for (i = 0; i < 8; i++) //$$anonymous$$gh score check

rather than

  for (i = 0; i > 8; i++) //$$anonymous$$gh score check

The ">" should be replaced with "<"

Comment
skwunk

People who like this

1 Show 1 · 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 skwunk · Aug 20, 2015 at 04:55 PM 0
Share

You're right, I had my operators flipped. Thanks, I guess it helps to have another set of eyes to look at the code to catch something like that. :)

avatar image

Answer by arun.pandey89 · Aug 19, 2015 at 03:24 AM

There are few loop holes .

1) Break the loop as soon as you get any index w$$anonymous$$ch has lesser value then $$anonymous$$gh-score (Coming from top to bottom). Why traverse bottom numbers they are definitely smaller as compared to $$anonymous$$gh-score and not your concern.

2) W$$anonymous$$le breaking loop sort array based on new $$anonymous$$gh-score .Also your re-arranging array logic is not correct . try t$$anonymous$$s :

 for (j = scoreArray.length - 1 ;  j > i ;  j-- )
                                       scoreArray [j] = scoreArray [j - 1];

Cheers Arun

Comment
skwunk

People who like this

1 Show 1 · 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 skwunk · Aug 20, 2015 at 04:51 PM 0
Share

Thanks so much, I didn't realize that I could use break to stop a for loop once it finds a value lower than the testScore. I also fixed my sorting logic to properly count down the array and moved the whole loop to the Start function and now it works the way I want it to.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to save an array to PlayerPrefs? 1 Answer

PlayerPrefsX IntArray 1 Answer

For In Loop Fills all values in arrays problem 2 Answers

Why is this giving me an error? (ToBuiltin problems) 1 Answer

Store Array in PlayerPrefX in Unity JS 1 Answer


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