Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making prefab as children of...

Discussion in 'Scripting' started by gasper133, Dec 15, 2015.

  1. gasper133

    gasper133

    Joined:
    Nov 5, 2015
    Posts:
    31
    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 :)

    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!");
    }
    }
     
  2. mikeymike

    mikeymike

    Joined:
    Oct 21, 2014
    Posts:
    35
    Code (csharp):
    1.  
    2. public class MakeMeACard : MonoBehaviour {
    3.  
    4. public Transform CardPrefab;
    5. public Transform HandGo;
    6.  
    7. void Start () {
    8. GameObject go = Instantiate(CardPrefab, new Vector3(400, -85, 4), Quaternion.identity) as GameObject;
    9. go.parent = HandGo;
    10. Debug.Log("Card made!");
    11.  
    12. }
    13. }
    14.  
    obviously youll need to link the transform of HandGo to your parent object
     
  3. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/
    Attach the transform of the instantiated object as a child of the hand object.
    Code (csharp):
    1.  
    2. public class MakeMeACard : MonoBehaviour
    3. {
    4.     public Transform hand;
    5.     public GameObject cardPrefab;
    6.  
    7.     private void Start ()
    8.     {
    9.         GameObject newCard = Instantiate(cardPrefab, new Vector3(400, -85, 4), Quaternion.identity) as GameObject;
    10.         newCard.transform.SetParent(hand, true);
    11.         Debug.Log("Card made!");
    12.     }
    13. }
    14.  
     
  4. gasper133

    gasper133

    Joined:
    Nov 5, 2015
    Posts:
    31
    How do I correctly "link" the transform to parent object?
    This must be what I'm doing wrong...
     
  5. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    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
     
  6. gasper133

    gasper133

    Joined:
    Nov 5, 2015
    Posts:
    31
    It works!! :D
    Thanks guys. I just needed to change your boolean in transform.SetParent to false from true and it works perfectly :)
     
  7. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207