- Home /
"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 scores; //The key is the facebook id, value is his score. private Dictionary fbProfileImages = new Dictionary ();
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.
Answer by NothAU · Jul 15, 2012 at 03:07 PM
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?
Answer by taforyou · Jul 15, 2012 at 11:18 PM
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 :)