Instantiate GameObject into UI Canvas

So, I try to instantiate one of my image, and try to set the position as I wish. However, it doesn’t work, and the position just went off. Here’s my code:

Start ()


 heartList = new List<Image> ();
 for (int i = 0;i < heartNum;i++)
      heart = Instantiate (heartPrefab) as Image;
      heart.transform.SetParent (canvas, false);
      heart.transform.localPosition = new Vector3 (heartX + (heartSpacingX * i), 0, 0);
      heartList.Add (heart); 


To change the properties of a RectTransform you use rect.sizeDelta to change the size of the object, and rect.anchoredPosition to change its position. Also take into account that this position is relative to the object anchor point. You can set the anchor point using rect.anchorMax and rect.anchorMin. For more information check: Unity - Scripting API: RectTransform