- Home /
[Solved] Assign an Instantiated object to a parent
Hi all! Still very new to development, but I'm in need of some guidance. I am looking to take a randomly drawn card, and add it to an existing parent (a plane on my UI). However, I'm unsure how to assign the new card to a specific parent. I copied the code from a tutorial, but I want to change it up a bit slightly. I'm trying to add the card to the "Hand" parent.
Can somebody help walk me through this? Below is the code, and I have attached a screenshot of my editor:
void MoveDealtCard()
{
GameObject newCard = DealCard();
// check card is null or not
if (newCard == null) {
Debug.Log("Out of Cards");
showReset = true;
return;
}
//newCard.transform.position = Vector3.zero;
newCard.transform.position = new Vector3((float)cardsDealt / 4, (float)cardsDealt / -4, (float)cardsDealt / -4); // place card 1/4 up on all axis from last
hand.Add(newCard); // add card to hand
cardsDealt ++;
}
Hi, the GameObject's hierarchy is managed through the Transform component. Check the function with "parent", "child" and "sibling" in it. A quick search with those functions name in the search should give plenty of result and help :)
Thank you! This is the guidance I need - still learning all of the functions of C#
Answer by rageingnonsense · Apr 29, 2015 at 03:08 PM
newCard.transform.SetParent(handTransform);
handTransform in this context is the transform of the Hand GameObject. You could use Find to get this, but I suggest caching it in the script and setting it in the inspector
First, thank you for helping me out! Can't begin to explain how much this forum has been helpful, and I can't wait to help contribute. However, can you help me understand the (handTransform) piece? The code doesn't recognize this - what would I need to change in order to set this?
That is a placeholder variable. See, "Hand" in your hierarchy is a gameobject. There are many ways to reference a game object in your scene within your code. The best practice though I$$anonymous$$O os to do the following:
add this variable to your script: "public Transform handTransform;"
save script and wait for recompile
you should now see in the inspector a new variable you can assign for this script. drag the "Hand" gameobject to this placeholder in the inspector.
handTransform is now set, and it is set to the "Hand" gameobject (or more correctly, the transform of the gameobject).
Now you can access the Hand GameObject in this script (via its transform). In this case, we are using it as the parameter for the SetParent method. We are saying "$$anonymous$$ake the Hand transform the parent of this Card"
This is just one way of doing it though. This is all very basic Unity 101 stuff. I suggest you explore the documentation on GameObject and Transform. You use these classes CONSTANTLY (especially in the case of transform).
Thank you very much! This worked like a charm - I am trying to familiarize myself and I will continue to read on documentation. The only issue is that the instantiated object is not matching the size of the other cards. I tried adding a layout element to force it to match the size of the cards, but it still isn't working. I'll continue to investigate and see if I can figure this one out.
I think I figured it out - thank you very much for the help rageingnonsense!
In case anybody is curious about the answer to this, the issue was with the Canvas scaling. $$anonymous$$y canvas was scaled at "0.5" ins$$anonymous$$d of 1, so the instantiated objects looked to be larger than all other gameobjects. Once I switched the Canvas scaling to "1", everything worked as expected.
Thanks all for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613194303im_/https://answers.unity.com/themes/thub/images/avi.jpg)