Search Unity

display data from php to unity

Discussion in 'Multiplayer' started by Alex1McGrady, Sep 3, 2017.

  1. Alex1McGrady

    Alex1McGrady

    Joined:
    Mar 30, 2016
    Posts:
    48
    Hello,I want to display my player stats from my database to my unity game.The thing is when i run the game in the editor everything is fine but when i'm playing it on my android phone the stats are not there.Here is my script

    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;
    15.     public string warriorname;
    16.     public string dbUser;
    17.     public string dbMoney;
    18.  
    19.     string answer;
    20.     public string[] stats;
    21.     string url = "http://ptixiaki.sotirisoikonomou.com/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");
    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]);
    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];  //emfanizei apo tin vasi mou ta stoixeia
    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.  
    80. }
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    You probably want to set your labels when you get a response and not in the OnGUI method which is to be used for the legacy GUI system.

    In addition, more information is needed. What's not working? Is it a UI problem? Or are you not getting a response?