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},
click 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[] { "
" }, 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.