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;
}
}
}