How can I find an object in an array by name?

Hey guys,

So I’m making something for my game, i set-up items in an array called “Questboxitems” I really need to find certain things in the array by name, as they’re loaded into this array at random.

Here’s the code i have:

QuestBoxItems [GameObject.Find("Text")].GetComponent<Text> ().enabled = true;

It doesn’t work due to Unity not being above to convert an Int to a String, is there a way around this?

Could you do something like this?

foreach(GameObject go in QuestBoxItems)
{
    if(go.name == "Text")
    {
        go.GetComponent<Text>().enabled = true;
        break;
    }
}

One way of doing it would be to set up a custom function that handles this kind of thing, i.e.

GameObject GetQuestBoxItem (GameObject[] g, string name)
{
    for (int i = 0; i < g.Length; i++) 
    {
        if (g*.name == name)*

return g*;*
}

Debug.Log (“No item has the name '” + name + “'.”);
return null;
}
And then if you had to find that item, all you would do is go;
GameObject item = GetQuestBoxItem (QuestBoxItems, “Text”);

if (item)
item.GetComponent().enabled = true;
else
Debug.Log (“Could not find item.”);