Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Claymstamper · Dec 24, 2016 at 07:38 PM · gameobjectinstantiatetransformchildchild object

Instantiating a new gameObject as a child of a different gameObject

here is the code public void FillNextOpenSpot()

   {
     GameObject newSelected = Instantiate (selected, openSpotPos, Quaternion.identity) as GameObject;
     newSelected.transform.SetParent (nextOpenSpot.transform); // tried this patent
     newSelected.transform.parent = nextOpenSpot.transform; // and tried this one too
 }


nextOpenSpot is a game object that I have declared globally. The object is Instantiating but it's just going straight to the top of the hierarchy. One weird thing that I found in troubleshooting is that it will child to the object the code is attached to if I changed the right side of the assignment of either patent to just "Transform". It only doesn't work when I try to specify a different transform.

Comment
Add comment · Show 1
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 tanoshimi · Dec 24, 2016 at 07:41 PM 0
Share

That would rather suggest that nextOpenSpot.transform is null. Have you tried adding a Debug.Log to verify that it is not?

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by RobAnthem · Dec 25, 2016 at 06:30 PM

Just to see if it changes anything, you could try

 newSelected.transform.SetParent (nextOpenSpot.GetComponent<Transform>());


Comment
Add comment · Show 1 · 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 Claymstamper · Dec 27, 2016 at 05:25 PM 0
Share

Omg thank you so much. You seriously just saved me so much stress. here's my finished script using that patent if you're interested

    {
     Transform newParent = nextOpenSpot.GetComponent<Transform> ();
     GameObject newSelected = Instantiate (selected, openSpotPos, Quaternion.identity) as GameObject;
     newSelected.transform.SetParent (newParent);
 }

thanks a lot! Happy holidays my friend

avatar image
0

Answer by jmgek · Dec 24, 2016 at 08:01 PM

It is transform.parent, I don't know where you're getting nextOpenSlot but it has to be a game object in the scene for it to be a parent:

         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         GameObject sphere2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
 
         sphere2.transform.parent = sphere.transform;
Comment
Add comment · Show 2 · 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 Claymstamper · Dec 24, 2016 at 08:29 PM 0
Share

nextOpenSpot is a game object. I

avatar image jmgek Claymstamper · Dec 25, 2016 at 07:16 AM 0
Share

Sorry, then what's the problem?

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

Instantiate Terrain Object as child of Empty Game Object 1 Answer

Getting instance of an sub object rather than the original's subobject 0 Answers

Finding Children question 3 Answers

Instantiate object as child of exsisitng game 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