Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

IndexOutOfRangeException

Discussion in 'Scripting' started by Alex1McGrady, Aug 17, 2017.

  1. Alex1McGrady

    Alex1McGrady

    Joined:
    Mar 30, 2016
    Posts:
    48
    Hello guys!I am trying to log in to my database with unity and display my player stats from my database table to my unity scene.The problem is i can display all the stats i need except the name of the player and the error is
    IndexOutOfRangeException
    Here is my c# code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class displayDataFromPhp : MonoBehaviour
    6. {
    7.     public static displayDataFromPhp _INstance;
    8.     public Text namee;
    9.     public Text lvl;
    10.     public Text atc;
    11.     public Text def;
    12.     public Text cash;
    13.     public Text hp;
    14.     private GameObject _Manager; // gia na allazei tis metavlites otan agorazei apo to shop o xristis!
    15.     public string warriorname;
    16.     public string dbUser;
    17.     public string dbMoney;
    18.  
    19.     string answer;
    20.     public string[] stats;
    21.     string url = "http://localhost/CodeIgniterUnity/index.php/Users/guser";
    22.  
    23.  
    24.     public void Start()
    25.     {
    26.         if (_INstance == null)
    27.         {
    28.  
    29.             _INstance = this;
    30.             DontDestroyOnLoad(this.gameObject);
    31.             _Manager = GameObject.Find("playerStats"); // gia na allazei tis metavlites otan agorazei apo to shop o xristis kai na uparxei to object se oles tis skines!
    32.         }
    33.         else if (_INstance != this)
    34.         {
    35.             Destroy(gameObject);
    36.         }
    37.  
    38.         WWWForm www = new WWWForm();
    39.         www.AddField("user", internetCalls._Instance.LoggedInUsername);// stelno sto arxeio tis php(contoller users)stin function guser to global username pou exo parei gia na dei an uparxei stin vasi
    40.         WWW get_www = new WWW(url, www);
    41.         StartCoroutine(WaitForRequest(get_www));
    42.  
    43.     }
    44.     //Our Coroutine for getting the data
    45.     IEnumerator WaitForRequest(WWW www)
    46.     {
    47.         yield return www;
    48.        
    49.         // check for errors
    50.         if (www.error == null)
    51.         {
    52.  
    53.             //Assign the data that was fetched to the variable answer
    54.             answer = www.text.ToString();
    55.             stats = answer.Split(","[0]); // PAIRNEI APO TON CONTROLLER TIS PHP STIN FUNCTION guser TO $data kai opou vlepei ',' to vazei se ena keli tou pinaka
    56.             warriorname = stats[0];//to name
    57.             dbUser = stats[6]; // to username
    58.             dbMoney = stats[4]; // ta lefta
    59.         }
    60.         else {
    61.             Debug.Log("WWW Error: " + www.error);
    62.          
    63.         }
    64.     }
    65.  
    66.     //GUI Components
    67.  
    68.     void OnGUI()
    69.     {
    70.         namee.text = stats[0];  
    71.         lvl.text = stats[1];
    72.         atc.text = stats[2];
    73.         def.text = stats[3];
    74.         cash.text = stats[4];
    75.         hp.text = stats[5];
    76.  
    77.     }
    78.  
    79. }
    I use the split function to take the user stats and put them in my texts.
    The error is in the namee.text = stats[0]; but if i debug.log (stats[0]) in the IEnumerator WaitForRequest(WWW www) it shows me the name!

    The sword should had the user name on it!


    Here is my scene objects


    The red circle is the name that i can't display!

    Any ideas?
     
  2. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    Code (CSharp):
    1. void OnGUI()
    2.     {
    3.  
    4. if(stats == null) return;
    5.  
    6.         namee.text = stats[0];
    7.         lvl.text = stats[1];
    8.         atc.text = stats[2];
    9.         def.text = stats[3];
    10.         cash.text = stats[4];
    11.         hp.text = stats[5];
    12.     }
     
  3. Alex1McGrady

    Alex1McGrady

    Joined:
    Mar 30, 2016
    Posts:
    48
    Hey Simo!Thank you for your reply.I will test it later because i'm at work right now!Could you please explain me what do you mean by that?Because the lvl atc def cash and hp are being displayed already.I didn't understand the meaning of the return.
     
  4. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    because OnGUI is called even if WaitForRequest doesnt finish yet, so until then stats is null
     
  5. Alex1McGrady

    Alex1McGrady

    Joined:
    Mar 30, 2016
    Posts:
    48
    oh i hope that's it then!I will tell you by the end of the day!Thank you so much!
     
  6. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    probably you need more than this test "if(stats == null) return;", may be its already set in the inspector, I thnik you need to check stats.length as well
     
  7. Alex1McGrady

    Alex1McGrady

    Joined:
    Mar 30, 2016
    Posts:
    48
    The weird thing is that it returns everything except the name :p which is the first element of the array (and it says
    IndexOutOfRangeException) :p