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)”.