Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by taforyou · Jul 15, 2012 at 02:23 PM · arraywwwfacebooksplit

"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.

Comment
Add comment
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
0

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?

Comment
Add comment · 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
0

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 :)

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best way to convert a string list to a class list? 1 Answer

UnityEngineInternal.WebRequestUtils.MakeInitialUrl throwing IndexOutOfRangeException 3 Answers

Post image to facebook wall 0 Answers

Split each number in an int without converting to char and string first? 1 Answer

split() for separators more than 1 characters 2 Answers


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