x


"Array index is out of range" Please Help !!!

using UnityEngine; using System.Collections; using System.Collections.Generic; using System;

public class DiceGame : MonoBehaviour { private const int max = 6; private string btnText = "Press to roll"; public static string FacebookId = "1"; private Dictionary<string, int> scores; //The key is the facebook id, value is his score. private Dictionary<string, Texture2D> fbProfileImages = new Dictionary<string,Texture2D>();

void Start()
{
    scores = new Dictionary<string, int>();
    StartCoroutine(retrieveHighscores());
    fbProfileImages = new Dictionary<string, Texture2D>();
}

void OnGUI()
{
    if (GUI.Button(new Rect(10, 10, 200, 50), btnText))
    {
        int score = UnityEngine.Random.Range(1, max + 1);
        btnText = string.Format("You rolled {0}, \nclick to try again.", score);

        StartCoroutine(submitHighscore(score, FacebookId));
    }

    float h = 10;
    foreach (var score in scores)
    {
        GUI.Label(new Rect(300, h, 200, 50), score.Value.ToString());

        if (fbProfileImages != null && fbProfileImages.ContainsKey(score.Key))
            GUI.DrawTexture(new Rect(230, h, 50, 50), fbProfileImages[score.Key]);

        h += 60;
    }
}

IEnumerator submitHighscore(int score, string fbid)
{
    WWW webRequest = new WWW("http://localhost/insert_highscore.php?score=" + score + "&fbid=" + fbid);
    yield return webRequest;

    yield return retrieveHighscores(); //After we submit we'd want an update, perhaps someone else played too!
}

IEnumerator retrieveHighscores()
{
    WWW webRequest = new WWW("http://localhost/get_highscores.php");
    yield return webRequest;

    string[] lines = webRequest.text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

    scores = new Dictionary<string, int>(); //Always reset our scores, as we just got new ones.

    foreach (string line in lines)
    {

        string[] parts = line.Split(',');

        string id = parts[0];
        int score = int.Parse(parts[1]);


        if (!fbProfileImages.ContainsKey(id)) //Have we already loaded this user before?
        {
            //No, better get our image!
            WWW imageRequest = new WWW("http://graph.facebook.com/" + id + "/picture");
            yield return imageRequest;
            fbProfileImages.Add(id, imageRequest.texture);
        }

        scores.Add(id,score);            
    }                
}

}

In line 66 which is "int score = int.Parse(parts[1]);" is error array index is out of range. i've already find other similar topic but i can't slove this problem and i can't Build my project. But it's work if i put variable manually "int score = 999;" which is not the point of using split array

Please help me rewrite the code

thank you.

more ▼

asked Jul 15 '12 at 02:23 PM

taforyou gravatar image

taforyou
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

i will make sure that i'm getting a response from "get_highscores.php" and i'll tell you when i can verify. Thank you for your answer :)

more ▼

answered Jul 15 '12 at 11:18 PM

taforyou gravatar image

taforyou
1 1 1 2

(comments are locked)
10|3000 characters needed characters left

I'm not sure it's an issue with the code, are you certain the script is getting a response from the "get_highscores.php" file? And, if it is, is it in the right format?

more ▼

answered Jul 15 '12 at 03:07 PM

NothAU gravatar image

NothAU
30

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1396
x546
x140
x85

asked: Jul 15 '12 at 02:23 PM

Seen: 665 times

Last Updated: Jul 15 '12 at 11:18 PM