Can't set source images for instantiated gameobjects

Hi there,

I’ve been tinkering with Unity for about three weeks now and I’m kind of stuck on this.
I’m able to set an image on an already existing gameobject with this code:

private Image image;
private Sprite uni;

private void Start()
{
        image = GetComponent<Image>();
        uni = Resources.Load<Sprite>("8");
        image.sprite = uni;
}

However when I try it in a loop from an onClick event, everything is created, except the images:

public GameObject Card;
public Image image;
public Sprite uni;
public GameObject Player1;

public void OnClick()
{
        Player1 = GameObject.Find("Player1");
        for (int i = 1; i < 6; i++)
        {
            GameObject newCard = Instantiate(Card, new Vector3(0, 0, 0), Quaternion.identity);
            newCard.name = i.ToString();
            newCard.transform.SetParent(Player1.transform, false);
            image = newCard.GetComponent<Image>();
            uni = Resources.Load<Sprite>("8");
            image.sprite = uni;
        }
    }

The gameobjects are created, the names are giving (just numbers), but the Source Image for each stays on “None (Sprite)”.

You getting any errors?

No errors, just no images.

Have you checked that the value of uni isn’t null after it is assigned at line 16, maybe the resource isn’t being loaded now for some reason.

Did a debug.log off the uni and I got indeed a null:

Null
UnityEngine.Debug:Log (object)

So it is being loaded in my first example, but not in the second. It is however the same project, so I guess Resources.Load doesn’t work with intantiated objects?

Resources.Load has no idea what object you’ll use the thing you load on.

Does your first example still work? If you provide reference to your sprite via the Inspector instead of via Resources.Load, does that work?