How to change the image of an instantiated prefab in the UI by mouse click on a game object in the s

Hello everyone,

Im working on an inventory for my simple hidden object game. I want to change the background of the icon in the inventory when I collect the hidden object in the scene (for instance when I collect the Jelly, I want to change the background of the jelly icon to an other background:)).

Im collecting the objects with interface, which is attached to the game object.

Its looks like this:

using UnityEngine;
using System;

public class Jelly : MonoBehaviour, ICollectible
{
    public static event Action OnJellyCollected;

    public void Collect()
    {
        Debug.Log("You Collected The Jelly");
        OnJellyCollected?.Invoke();
    }

}

The inventory in the scrollview is populated by instantiating the icon prefab, with scriptable objects.
The following script is attached to the scrollview content:

using UnityEngine;

public class InventoryPopulator : MonoBehaviour
{
 
    public GameObject prefab;
    public Transform content;

    public InventoryItemData[] items;

    private void Start()
    {
        foreach(InventoryItemData inventoryItemData in items)
        {
            GameObject instantiatedPrefab = Instantiate(prefab);
            instantiatedPrefab.transform.SetParent(content, false);
            instantiatedPrefab.GetComponent<InvnetoryIconUI>().inventoryItemData = inventoryItemData;
            instantiatedPrefab.GetComponent<InvnetoryIconUI>().SetIconUI();

           
        }
    }
}

And this one is attached to the icon prefab:

using UnityEngine;
using UnityEngine.UI;

public class InvnetoryIconUI : MonoBehaviour
{
    public InventoryItemData inventoryItemData;

    [SerializeField]
    private Image icon;

    [SerializeField]
    private Image iconBackground;

    public void SetIconUI()
    {
        icon.sprite = inventoryItemData.icon;
        iconBackground.sprite = inventoryItemData.iconBackground;
    }

}

It works great, but can you please someone help me to implement or update the code in a right way to change the icon background on collecting the scene object?

any help will be appreciated!

Thanks a lot.

Attached is a fully-operational example of dynamically instantiating little “things” such as inventory elements.

Beyond that, here are some general inventory notes:

These things (inventory, shop systems, character customization, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

They contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation

Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

8723331–1179720–DynamicUIDemo.unitypackage (96.1 KB)