List<> problem
Hello comm unity) I got code, where I'm trying to get all children of an object to array, then get the children of a child objects in array to) But I've got problem with List<>. So here's the code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class SearchChildren : MonoBehaviour {
Text textFinder;
string countFinder;
string countTextFinder;
GameObject[] counter;
float sum;
float countFinderNumber;
List<string> textFinderArr = new List<string>();
public void countResults ()
{
counter = new GameObject[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
{
counter[i] = transform.GetChild(i).gameObject;
textFinderArr.Add(counter[i].transform.FindChild("Result").GetComponent<Text>().text);
countFinder = Regex.Match(textFinderArr[i], @"\d+").Value;
countFinderNumber = float.Parse(countFinder);
sum += countFinderNumber;
}
Debug.Log(sum);
}
}
So, the problem is that textFinderArr.Add(counter[i].transform.FindChild("Result").GetComponent().text); geets only 1 element, and I can not understand why. So please explain it to me, cause I'm kinnda feel dumb)
Comment
Best Answer
Answer by Losteone · May 04, 2016 at 11:56 AM
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class SearchChildren : MonoBehaviour {
Text textFinder;
string countFinder;
string countTextFinder;
GameObject[] counter;
float sum;
float countFinderNumber;
List<string> textFinderArr = new List<string>();
public void CountResults()
{
sum = 0;
counter = new GameObject[transform.childCount];
for (int i = 0; i < transform.childCount; i++)
{
counter[i] = transform.GetChild(i).gameObject;
textFinderArr.Insert(i, counter[i].transform.FindChild("Result").GetComponent<Text>().text);
countFinder = Regex.Match(textFinderArr[i], @"\d+").Value;
countFinderNumber = float.Parse(countFinder);
sum += countFinderNumber;
}
Debug.Log(sum);
}
}