Making an array to find objects with NAME

Hey there! I’m trying to make an array to find objects by name.

Right now I have the following code, which works fine now but won’t work when a username adds new elements to the array:

findWithName [0] = GameObject.Find ("1");
        findWithName [1] = GameObject.Find ("2");
        findWithName [2] = GameObject.Find ("3");
        findWithName [3] = GameObject.Find ("4");
        findWithName [4] = GameObject.Find ("5");
        findWithName [5] = GameObject.Find ("6");
for (int i = 0; i < arrayI; i ++) 
    {
      findWithName [i].GetComponentInChildren<Text> ().text = SaveTest.stringArray[i];
    }

This is why I made the following, the problem is that I CAN’T implicitly convert type ‘void’ to ‘UnityEngine.GameObject’ and I totally understand why but don’t know how to do a for loop to search in an array by the name:

for (int i = 0; i < arrayI; i ++)
    {
        findWithName [i] = caseFind(i);
        findWithName [i].GetComponentInChildren<Text> ().text = SaveTest.stringArray[i];
    }

    }
    public void caseFind(int i)
    {
        switch(i)
        {
        case 0:
            GameObject.Find("1");
            break;
        case 1:
            GameObject.Find("2");
            break;
        case 2:
            GameObject.Find("3");
            break;
        case 3:
            GameObject.Find("4");
            break;
        case 4:
            GameObject.Find("5");
            break;
        case 5:
            GameObject.Find("6");
            break;
        case 6:
            GameObject.Find("7");
            break;
        case 7:
            GameObject.Find("8");
            break;
        case 8:
            GameObject.Find("9");
            break;
        case 9:
            GameObject.Find("10");
            break;
        default:
            Debug.Log("none");
            break;
        }

    }

you need to return it as GameObject so it needs to be public GameObject caseFind(int i)

in case you need
return GameObject.Find(“1”);

you dont need break even tho i dont understand your array completely

Your code seems like a very strange thing to do but I think this is what you want

for (int i = 0; i < arrayI; i ++)
    {
      GameObject.Find(i.ToString()).GetComponentInChildren<Text> ().text = SaveTest.stringArray[i];
    }

Also not sure why you think your code sample 2 is better than code sample 1. Sample 1 is faster to execute and easier to read. Assigning the values to the dictionary is a reasonable approach - even better than the code I gave. This is a type of registration and could be done in the Start method to cache the gameobjects you need.

1 Like

Great, that works too.

Im leaving the workarount here:

for (int i = 1; i <= arrayI; i ++)
        {
            GameObject.Find(i.ToString()).GetComponentInChildren<Text> ().text = SaveTest.stringArray[i-1];
        }

Erased the switch, at the end of the line I added a -1 to search for the first element in the array which is 0, while the first element to look for as a GameObject is “1”.

Thank you!

where are you from hrvat?

Mexico :slight_smile:

Oh, I am from croatia and hrvat here means croatian so i tought you were from croatia too

I was born there! I’m actually croatian/mexican. I’ve lived in Mexico almost all my life.

hahahahh
atleast you know your heritage
how about language hahahaha