- Home /
Trying to get an array to show up in Debug.log
I'm trying to script a high 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 anything is even being entered. Not even the Inspector shows any indication that anything 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++) //high 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.
Answer by Bakshish-Singh · Aug 19, 2015 at 05:01 AM
I think there is some problem with your code you posted. The for loop on line 12 should be
for (i = 0; i < 8; i++) //high score check
rather than
for (i = 0; i > 8; i++) //high score check
The ">" should be replaced with "<"
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. :)
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 which has lesser value then high-score (Coming from top to bottom). Why traverse bottom numbers they are definitely smaller as compared to high-score and not your concern.
2) While breaking loop sort array based on new high-score .Also your re-arranging array logic is not correct . try this :
for (j = scoreArray.length - 1 ; j > i ; j-- )
scoreArray [j] = scoreArray [j - 1];
Cheers Arun
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
![](https://koobas.hobune.stream/wayback/20220612000459im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Store Array in PlayerPrefX in Unity JS 1 Answer
assigning arrays with for in 1 Answer
Attempting to store an Inventory Array in PlayerPrefsX 1 Answer
Saving Array Objects in Android. 0 Answers
PlayerPrefsX Question 1 Answer