Parse issue, despite the data being pulled from database
Hello all!
I am in need of some help, for the past two or three days I have been pulling my hair out on a parse issue.
To give you a quick run down, I am trying to work on a Rank system. Basically, the game connects to a database, gets the data so that I can set the rank image based off of how much XP a player has. I.E if the player has over 1000XP but less than 2000 he will be at rank 3 for example. The data in the database is text. I am trying to convert it to a int.
I can get and set the data no problem, the connection works and I can see the value in the inputfield text when I pause the game. So I know its working. However, I keep getting this error: "FormatException: Input string was not in the correct format System.Int32.Parse" we all know how it ends. I even added a debug.log and it isn't helping me at all. I have tried so many variations of code from my several days of googling. I found a lot of topics about this issue, but nothing exactly as to what I am facing. I have checked the text of the inputfield and there are no crazy characters or spaces or unknown variables.
Please excuse the mess, I am not the best coder, and I have gone through so many variations just to try and get it to work, before I can clean it up.
void OnJoinedLobby()
{
Debug.Log("We joined the lobby.");
StartCoroutine(Fade(LobbyState.MainMenu));
ChangeWindow(2, 1);
getrank();
// getrank();
//Data 1 i s XP for now
// int xpdata2 = Int.
// if (xpdata2 > 1)
// {
// PlayerRank.SetRank1();
// }
// GetFromServer.Data1.text = RANKSXP.ToString();
// GetFromServer.Data2.text = RANKSXP.ToString();
// GetFromServer.Data3.text = RANKSXP.ToString();
}
public static Image Rank1;
private static LoginPro_GetFromServer GetFromServer;
public static void getrank()
{
// Rank1 = GameObject.FindWithTag("RankImage").GetComponent<RawImage>();
Rank1 = GameObject.FindWithTag("RankImage").GetComponent<Image>();
GetFromServer = GameObject.FindWithTag("RanksTagGet").GetComponent<LoginPro_GetFromServer>();
GetFromServer.GetFromServer();
string ranktest = GetFromServer.Data1.text.ToString();
try
{
int xpdata2 = int.Parse(GetFromServer.Data1.text);
if (xpdata2 >= 100)
{
Rank1.sprite = Resources.Load<Sprite>("Bronze0") as Sprite;
}
{
}
}
catch
{
Debug.Log(">" + GetFromServer.Data1.text + "Ranktest: " + ranktest.Length);
}
//int xpdata2 = int.Parse(GetFromServer.Data1.text);
}
Please also excuse some of the commented failure tests. Its supposed to set the image based on the XP value given from the database. I have also made sure to set the inputfield as integer number. I have tried both. I have even tried custom. I have tried TryParse. Any help would be appreciated. Thank you guys so much!
I am sorry I had to ask a question at all, but I am really lost.
Answer by Bilelmnasser · Sep 29, 2017 at 08:49 AM
hi, seems your GetFromServer.Data1.text is empty already, check your GetFromServer.GetFromServer(); function or post code to get the help
Hi Bilemnasser!
Thanks for the reply! So that I am on the same page, you think that the GetFromServer.Data1.text is actually empty before it tries to parse? Because it does appear to grab it at some point. The screen shot above your post shows the data in the database, and the inputfield text is on the right. Here is the GetFromServer function:
using UnityEngine;
using UnityEngine.UI;
namespace LoginProAsset
{
/// <summary>
/// This class allow you to understand how to get datas from the server :
/// </summary>
public class LoginPro_GetFromServer : $$anonymous$$onoBehaviour
{
// All the datas we want to get from the server
public InputField Data1;
public InputField Data2;
public InputField Data3;
// public Text Data1;
//public Text Data2;
// public Text Data3;
// This is just here to show a popup when datas are sent (or error occured)
public UIAnimation_Alert Popup;
private void Awake()
{
DontDestroyOnLoad(this);
}
/// <summary>
/// Get datas from the server
/// Call methodForSuccess if data sent correctly
/// Call methodForError if data not sent
/// </summary>
public void GetFromServer()
{
// Here datas are null since we don't want to SEND anything (but we want something)
LoginPro.$$anonymous$$anager.ExecuteOnServer("GetData", GetFromServer_Success, GetFromServer_Error, null);
}
public void GetFromServer_Success(string[] datas)
{
Debug.Log("Success! The server answered : " + datas[0]);
Popup.Show("Success! The server answered : " + datas[0], 3);
Data1.text = datas[1];
Data2.text = datas[2];
Data3.text = datas[3];
}
public void GetFromServer_Error(string error$$anonymous$$essage)
{
Debug.LogError(error$$anonymous$$essage);
Popup.Show("Error : " + error$$anonymous$$essage, 5);
}
}
}
I should have mentioned in the previous post that the login functions are handled by an asset I purchased. But as the screen shots show, the data is eventually getting to the GetFromServer.Data1.text.
Answer by GM_Acid · Oct 02, 2017 at 06:04 PM
@Bilelmnasser You were correct. It appears as though the method isn't giving it any time to grab the data. I thought I had moved the getrank method somewhere else to give it time but did not work. I tried again and moved it to a different spot and it seems to work. I can pull data all day long now. I use that method at a different point on a different script (not rank related) and I have run into the same issue.
I looked into commands that would delay the function from moving forward but I have had no luck. It seems to steamroll right past it. Do you know of a quick way to sort of pause the method until it has retrieved the data?
Again I really appreciate everything.
use Yield statement using WWW Class or Yield statement using UnityWebRequest
example with UnityWebRequest :
using UnityEngine;
using System.Collections;
using UnityEngine.Experimental.Networking;
class $$anonymous$$yBehaviour: public $$anonymous$$onoBehaviour {
void Start() {
StartCoroutine(GetText());
}
IEnumerator GetText() {
UnityWebRequest www = UnityWebRequest.Get("http://www.my-server.com");
// yield return www.Send(); This line will wait till the data is retrived put what ever code to be executed after this line
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
}
Your answer
Follow this Question
Related Questions
How to instantiate (MonoBehaviour) script multiple times and call void? 1 Answer
How do you copy an image from the clipboard into an ui image? 0 Answers
Can someone tell me what is wrong with this script? 1 Answer
Errors in script when trying to play animation clips 1 Answer
Hi, I have two errors. I am sorry everyone but I am going crazy 1 Answer