Change variable names based on loop counter

Hey Guys,
im trying to set a GameObject to another Object within a Loop.
I’ve got my variable definitions:

public GameObject PreviewObjectID0;
    public GameObject PrefabObjectID0;
    public GameObject PreviewObjectID1;
    public GameObject PrefabObjectID1;
    public GameObject PreviewObjectID2;
    public GameObject PrefabObjectID2;
    public GameObject PreviewObjectID3;
    public GameObject PrefabObjectID3;
    public GameObject PreviewObjectID4;
    public GameObject PrefabObjectID4;
    public GameObject PreviewObjectID5;
    public GameObject PrefabObjectID5;

Later I’m trying to set those to some other Objects within a for loop (in the Update() function) based on a KeyDown:

for (int i = 0; i < 10; i++) {
                if (revid1 == i) {
                    PlayerController.GetComponent<Buildingsystem>().previewrev = GameObject.Find("PreviewObjectID" + i);
                    PlayerController.GetComponent<Buildingsystem>().prefabrev = GameObject.Find("PrefabObjectID" + i);
                    toolSelected = -1;

                    break;
                }
            }

Now I got the problem that the GameObjects (PrefabObjectID1,2,3, … and PreviewObjectID1,2,3, …) will be set to empty …
Please help me, I’m kinda frustrated :confused:

This is what Arrays are for…

[System.Serializable()]
public struct BuilderData
{
    public GameObject PrefabObject;
    public GameObject PreviewObject;
}

public BuilderData[] Data; //should use a more descriptive name, but from what you show I know not what you're describing


//... elsewhere:

PlayerController.GetComponent<Buildingsystem>().previewrev = Data.PreviewObject;

Note I also created a struct to go with the array because you had paired data. Some people create 2 arrays… I prefer the struct to emphasize both are necessary. With 2 arrays how does one ensure that there is a 1 to 1 relation when it’s being edited via the inspector with out a lot of extra legwork.