- Home /
The question is answered, right answer was accepted
C# Array, GuiText, assign strings.
Hi,
Im getting an array from a PHP script from my server with 11 strings.
I have an array with 11 gameobject(with GUIText on them) and i want to assign the strings to the gameobjects.
So that the Guitext show the string info.
like Array_GameObjects[i] = Array_php[i];
C#
Kind Regards
Answer by Michael S · Oct 15, 2013 at 08:14 AM
I changed the code to this, and it works :)
IEnumerator GetData(WWW w)
{
if(Loading == true)
{
guiLoading.guiText.text = "Loading Data";
}
else if (Loading == false)
{
guiLoading.guiText.text = "";
}
if (w.error != null)
{
print("There was an error getting the high score: " + w.error);
}
else
{
Loading = false;
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"((""((?<token>.*?)()"")|(?<token>[^&&]+))(\s)*)", options);
string input = w.text;
var result = (from Match m in regex.Matches(input)
where m.Groups["token"].Success
select m.Groups["token"].Value).ToList();
int pos = 0;
for (int i = 0; i < result.Count(); i += 2, pos++)
{
GameObject currentGuiText = Instantiate(GuiPrefab, new Vector3(0,0,0), Quaternion.identity) as GameObject;
_list.Add(currentGuiText);
result[pos] = result[i];
}
result.RemoveRange(pos, result.Count - pos);
Debug.Log("result" +result.Count);
Debug.Log("list" + _list.Count);
for (int i = 0; i < result.Count(); i++)
{
_list[i].guiText.text = string.Format("{1}", i, result[i]);
Loading = false;
}
}
Answer by KiraSensei · Oct 03, 2013 at 01:55 PM
Here is the doc about GUIText.
if Array_GameObjects contains GUIText game objects, you need to access their text to change it. Try something like that :
GUIText myCurrText = Array_GameObjects[i];
myCurrText.text = Array_php[i];
I tried this:
IEnumerator GetData(WWW w)
{
info[0].guiText.text = "Loading Data";
yield return w;
if (w.error != null)
{
print("There was an error getting the high score: " + w.error);
}
else
{
string[] InfoResult = Regex.Split(w.text, "-");
foreach (string entry in InfoResult)
{
string[] TextData = entry.Split( '-' );
int phpStringLength = info.Length - 1;
for (int i = 0; i <= phpStringLength; i ++ )
{
info[i].guiText.text = TextData[i];
}
}
info[0].guiText.text = w.text;
info[1].guiText.text = w.text;
info[2].guiText.text = w.text;
info[3].guiText.text = w.text;
info[4].guiText.text = w.text;
info[5].guiText.text = w.text;
info[6].guiText.text = w.text;
info[7].guiText.text = w.text;
info[8].guiText.text = w.text;
info[9].guiText.text = w.text;
info[10].guiText.text = w.text;
}
}
But i get This Error : IndexOutOfRangeException: Array index is out of range. WWWController+c__Iterator1.$$anonymous$$oveNext () (at Assets/Scripts/WWWController.cs:104)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Array index help. 1 Answer
Playing a soundclip at random from an array. C# 2 Answers
create string array from xml 2 Answers