Card Display not working. It is just displaying the cardPrefab not the Cards in the list

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardManager : MonoBehaviour
{
public static CardManager instance;

[SerializeField]
private List<Image> cardObjects;

[SerializeField] private GameObject cardHolder, cardPrefab, dummyCardPrefab;

private int k;

private void Awake()
{
    if (instance == null)
    {
        instance = this;
    }
}

private void Start()
{
    for (int i = 0; i < 20; i++)
    {
        k = i;
        SpawnCard();
    }
}

void SpawnCard()
{
    GameObject card = Instantiate(cardPrefab);
    card.name = "Card" + k;
    card.transform.SetParent(cardHolder.transform);
    card.GetComponent<CardView>(). SetImg(cardObjects[k]);
    
}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CardView : MonoBehaviour
{
private Image img;

private void Awake()
{
    img = GetComponent<Image>();
}

public void SetImg(Image cardObjects)
{
    img = cardObjects;
}

}

Hi, there’s a problem in you Image List definition. For Unity, Image is the Image Component and Sprite is the actual image.

In your code, you are declaring a List of Image Components and then, on CardView you are just swapping the components, not setting the sprite.

A better way of doing it might be:

[SerializeField]
private List<Sprite> cardObjects;

[SerializeField] 
private GameObject cardHolder, cardPrefab, dummyCardPrefab;


private void Awake()
{
     if (instance == null)
     {
         instance = this;
     }
}
private void Start()
{

    int cardCount = cardObjects.Count;
     for (int i = 0; i < cardCount; i++)
     {
         SpawnCard(i);
     }
}
void SpawnCard(int index)
{
     GameObject card = Instantiate(cardPrefab, cardHolder.transform);
     card.name = "Card" + index.ToString();
     card.GetComponent<CardView>().SetImg(cardObjects[index]);
}

then:

public class CardView : MonoBehaviour 
{ 
    private Image img;

     private void Awake()
     {
         img = GetComponent<Image>();
     }
     public void SetImg(Sprite cardObjects)
     {
         img.sprite = cardObjects;
     }
}

I was trying to use GameObjects instead of sprites. Is there a way to do that?