Making prefab as children of...

Hey guys! I’m working on card game, and I totally lost my mind with prefab. The code bellow works, but makes CardPrefab independant object. I want it to be child of let’s say Hand GameObject, because I want to store cards in hand :slight_smile:

Any idea how to fix it? I went through some threads where this was already asked, but it didn’t work for me… I must be doing something wrong.

public class MakeMeACard : MonoBehaviour {

public Transform CardPrefab;

void Start () {
Instantiate(CardPrefab, new Vector3(400, -85, 4), Quaternion.identity);
Debug.Log(“Card made!”);
}
}

public class MakeMeACard : MonoBehaviour {

public Transform CardPrefab;
public Transform HandGo;

void Start () {
GameObject go = Instantiate(CardPrefab, new Vector3(400, -85, 4), Quaternion.identity) as GameObject;
go.parent = HandGo;
Debug.Log("Card made!");

}
}

obviously youll need to link the transform of HandGo to your parent object

Attach the transform of the instantiated object as a child of the hand object.

public class MakeMeACard : MonoBehaviour
{
    public Transform hand;
    public GameObject cardPrefab;

    private void Start ()
    {
        GameObject newCard = Instantiate(cardPrefab, new Vector3(400, -85, 4), Quaternion.identity) as GameObject;
        newCard.transform.SetParent(hand, true);
        Debug.Log("Card made!");
    }
}

How do I correctly “link” the transform to parent object?
This must be what I’m doing wrong…

Just use the code @ThermalFusion gave you, which should work fine. Instead of hand, put your Hand object reference, or drag Hand object into hand property from the MakeMeACard object in unity editor

It works!! :smile:
Thanks guys. I just needed to change your boolean in transform.SetParent to false from true and it works perfectly :slight_smile:

When using a function which you are not familiar with, please check the documentation so you can be 100% that you do or don’t need that “false”