How to get an image in script?

Basically I have an inventory panel, and when I pick an object I want its icon to appear in the next free slot. Ive set up empty images for the slots, but how will I assign an icon to them?

You can access an Image component in code with

GetComponent<Image>()

and then from there, you can access the sprite property. If you want to set an icon I suppose you could then load the image from the resources folder using something like

Resources.Load<Sprite>("path/to/sprite")
1 Like