Cannot change parent of object after instantiation

Hey all,

I’m trying to change the parent of an object after instantiation but keep getting an error saying i’m unable to set the parent
8818132--1200190--upload_2023-2-19_12-16-8.png

 public GameObject panel;
    public HandCard cardPrefab;
   
    public void AddCard(int index)
    {
        GameObject cardObj = Instantiate(cardPrefab.gameObject);
        cardObj.transform.SetParent(panel.transform);

    }

As a result I can see the new card has been created in the hierarchy view but it’s not been assigned a parent and can’t be seen in the game view.

8818132--1200193--upload_2023-2-19_12-20-31.png

Thanks in advance

My guess is that you are trying to set the object to the parent of something that exists in the prefab menu. I assume you are trying to set it to either HandPanel or PlayerContent, so try dragging one of those from the hierarchy to your script.

public GameObject panel;
public HandCard cardPrefab;
  
public void AddCard(int index)
{
    GameObject cardObj = Instantiate(cardPrefab.gameObject, panel.transform);
}