Access to variable's script attached to a prefab object

Hello everyone, I need your help.

I am trying to build an inventory.
I created a first script (not Scriptable) with empty variables. This script is attached to each prefab in the “Resources/Target” folder and contains all the information of the individual prefab (e.g., name, image).

Next, there is a second script in the scene that I attached to an empty GO. The purpose of this second script is to instantiate a prefab UI (created earlier and which I named “Item”) for each object in my “Resources” folder. “Item” has two other UI objects, “Icon” and “Object name” as children.

Now, I am trying to dynamically assign each variable name and image (defined by each script attached to the prefab of the objects in the Resources folder) of the respective object to the objects “Icon” and “Object name”.

This is the second script. Could you help me understand why it is not assigning the names and images to any Item?

Thank you.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GeneratingObjects : MonoBehaviour
{
    [SerializeField] public GameObject[] objectList;
    public Transform itemContent;
    public GameObject inventoryItem;
    // Start is called before the first frame update
    void Start()
    {
        objectList = Resources.LoadAll<GameObject>("Target");
        for (int i = 0; i < objectList.Length; i++)
        {
            ItemInformations informations = objectList[i].GetComponent<ItemInformations>();
                GameObject obj = Instantiate(inventoryItem, itemContent);
            var itemName = obj.transform.Find("Object Name").GetComponent<Text>();
            var itemIcon = obj.transform.Find("Icon").GetComponent<Image>();
                 itemName.text = informations.objectName;
                 itemIcon.sprite = informations.objectImage;
        }
    }
  
}

This is the problem with methods that require magic strings to work, as any small error can lead to hard to find bugs.

This inventoryItem object you’re instantiating, should have a component that holds a reference to these Text and Image components (through serialised fields assigned in the inspector). Then, you can provide this component a method that takes an ItemInformations as a parameter and it can handle all the necessary logic internally.

Additionally, both objectList and inventoryItem could be of the type of their respective components. So long as the component is on the root object of the prefab, you can still assign them in the inspector, which will remove unnecessary GetComponent calls.

So the code could be distilled down to:

[SerializeField]
private ItemInformations[] itemList; //assign in inspector here

[SerializeField]
private Transform itemContent;

[SerializeField]
private ItemUIComponent itemUIComponent; //this is the aforementioned UI component

private void Start()
{
    for (int i = 0; i < itemList.Length; i++)
    {
        ItemInformations information = itemList[i];
        ItemUIComponent component = Instantiate(itemUIComponent, itemContent);
        component.InitialiseUI(information); //aforementioned method on your custom UI component
    }
}

with ItemUIComponent being the aforementioned component you make for these inventory UI listings.

Though are these ItemInformations just to provide raw data on items? Would they be better done as scriptable objects?

Thanks for your help.

I saw that the problem wasn’t the script but the reference text and image to the root object yesterday!