Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nikk014 · Apr 29, 2015 at 02:51 PM · beginnerparentinstantiated

[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:alt text

     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 ++;
     }


unity-screen-shot.png (55.5 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _dns_ · Apr 29, 2015 at 03:05 PM 0
Share

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 :)

avatar image Nikk014 · Apr 29, 2015 at 03:15 PM 0
Share

Thank you! This is the guidance I need - still learning all of the functions of C#

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nikk014 · Apr 29, 2015 at 03:28 PM 0
Share

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?

avatar image rageingnonsense · Apr 29, 2015 at 04:06 PM 0
Share

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:

  1. add this variable to your script: "public Transform handTransform;"

  2. save script and wait for recompile

  3. 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.

  4. 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).

avatar image Nikk014 · Apr 29, 2015 at 09:13 PM 0
Share

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. alt text

card-draw.png (15.9 kB)
avatar image Nikk014 · Apr 29, 2015 at 09:15 PM 0
Share

I think I figured it out - thank you very much for the help rageingnonsense!

avatar image Nikk014 · Apr 30, 2015 at 02:06 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Child object not moving with parent 0 Answers

Object not Instantiating Correctly 1 Answer

How can I find a unique sibling/parent/child in a specific hierarchy and not from all the scene? 1 Answer

Make a simple tree 1 Answer

Move one object with another, without parenting it to the object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges