Edditing a public GameObject Var by String.

Ok so what i need is this:

I have a Inventory made out of gameobjects, so no GUI, each “Slot” gameObject will have a child object floating in it representing the item it holds.
Whenever the void UpdateInventory() is called i want each child object (Slot1,Slot2,Slot3) to check if there is a child object inside them, if there is i want the child object to be stored in the Slot1,Slot2,Slot3 Variables.

I know it sounds a bit weird… If you have any suggestions to do it differently i would love to hear them.

This is what i have:

    public class Inventory : MonoBehaviour {
    
    public GameObject Slot1;
    public GameObject Slot2;
    public GameObject Slot3;

void UpdateInventory(){

    foreach(Transform child in transform){
    	if(transform.GetChild (0) != null){
    		(GameObject)this.GetType().GetField (child.name).SetValue (child.transform.GetChild (0));
    			}
    		}
}

Hope i made atleast a little bit of sence in what i am trying to accomplish.
Thanks if you decide to help me.

Make a List to contain your inventory. It’s easier to iterate and test than having each slot be an individual item that is checked.

In general this is a poor system. You have unnecessary active GameObjects in the game which eats up memory (however small it is). Rather you should do all of the inventory management inside of the script and only spawn the Object if it needs to be displayed (i.e. the active item).