How to set prefab images from script?
I have an inventory panel with a grid layout. I instantiate slots in it. Slot is just an image. I made a prefab called Slot. I am adding the slots to the grid via script. How can I set the image in a script like below?
 for (int i=0; i< database.items.Count; i++) {
   Texture2D icon = database.items[i].icon);
   GameObject newSlot = Instantiate(slot) as GameObject;
   newSlot.transform.SetParent(GameObject.FindObjectOfType<Inventory>().gameObject.transform, false);
  //newSlot.getComponent<image??> = icon
 }
 
              Answer by Luya · Aug 30, 2015 at 11:45 AM
I found my problem.
First import UnityEngine.UI so you can do
 getComponent<image>
 
               Then load the sprite from root folder. My images were in a folder called Resources. I was trying to load it as
 Resources.Load("Resources/" + filename);
 
 
               while the path should have been just:
 Resources.Load(filename)
 
               It is strange but finally it worked.
Answer by robertoltrocha · Oct 28, 2017 at 08:44 PM
Hi, I have the same issue, could you detail your solution?
Your answer
 
             Follow this Question
Related Questions
How to make pointer using Image/Sprite using Kinect sensor ? 0 Answers
is there simple way to put random sprite to gameobject multi child with no repating. 1 Answer
How do I scale icons independently? 0 Answers
Images/Sprites are invisible in scene view 0 Answers
Trying to replace a sprite in UI Image 0 Answers