I have 10 Text objects tagged with “1stThrow”, 10 tagged with “2ndThrow”, and 10 tagged with “culScore”. I use the following lines of code to access these Text objects in my Unity project:
csharp
Copy code
GameObject[] firstThrowObj;
public Text[] firstThrow;
GameObject[] secondThrowObj;
public Text[] secondThrow;
GameObject[] totalObj;
public Text[] totalscore;
void Start()
{
firstThrowObj = GameObject.FindGameObjectsWithTag("1stThrow");
firstThrow = new Text[firstThrowObj.Length];
for (int i = 0; i < firstThrowObj.Length; i++)
{
firstThrow[i] = firstThrowObj[i].GetComponent<Text>();
}
secondThrowObj = GameObject.FindGameObjectsWithTag("2ndThrow");
secondThrow = new Text[secondThrowObj.Length];
for (int i = 0; i < secondThrowObj.Length; i++)
{
secondThrow[i] = secondThrowObj[i].GetComponent<Text>();
}
totalObj = GameObject.FindGameObjectsWithTag("culScore");
totalscore = new Text[totalObj.Length];
for (int i = 0; i < totalObj.Length; i++)
{
totalscore[i] = totalObj[i].GetComponent<Text>();
}
}
Issue:
When I try to update the text in the HandleFirstThrow
method using the line:
csharp
Copy code
firstThrow[throwCounter].text = throw.ToString();
I receive an “IndexOutOfRangeException” error.