• 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 SamSandeep · Mar 20, 2014 at 03:00 AM · child object

Load objects in runtime as children of other objects

Hi,

I'm trying to read data from a text file and load gameObjects accordingly in runtime. The objects are loading fine. The only problem is these objects are loaded in the root of the hierarchy.
I want them to be children of another empty gameObject I've created so that I can control them from another script attached to the empty object. Could anyone please help me.

Cheers, Sam

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 robertbu · Mar 20, 2014 at 03:01 AM 0
Share

Without code it is difficult to figure out where you have issues. All you have to do is set the transform.parent of your new object:

  newObject.transform.parent = emptyObject.transform; 
avatar image SamSandeep · Mar 20, 2014 at 05:26 AM 0
Share

This is basically what I'm doing.

 GameObject newGameObj;
 Transform newParent = GameObject.Find ("emptyObject").transform;
 
            for (int i = 0; i < 50; i++)
            {
              int xValue = Random.Range(0, 640);
 
              Instantiate(newGameObj, new Vector3(x, y, z), Quaternion.identity);
              newGameObj.transform.parent = newParent;
            }
 

But getting this error :

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform)

2 Replies

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

Answer by SamSandeep · Mar 20, 2014 at 05:45 AM

Silly me :P

I just had to instantiate the prefab and then change the parent, instead of changing the parent of the prefab itself.

     GameObject newGameObj;
     Transform newParent = GameObject.Find ("emptyObject").transform;
  
            for (int i = 0; i < 50; i++)
            {            
  
              GameObject tempObj =  Instantiate(newGameObj, new Vector3(x, y, z), Quaternion.identity);
              tempObj.transform.parent = newParent;
            }


Thanks all for sparing your time.. :D

Cheers

Comment
Add comment · 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
0

Answer by supernat · Mar 20, 2014 at 03:03 AM

 ObjectB.transform.parent = ObjectA.transform;

This will assign objectA as the parent of Object B, so just apply this to your scenario.

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 SamSandeep · Mar 20, 2014 at 05:26 AM 0
Share

This is basically what I'm doing.

 GameObject newGameObj;
     Transform newParent = GameObject.Find ("emptyObject").transform;
             
             for (int i = 0; i < 50; i++)
             {
                 int xValue = Random.Range(0, 640);
     
                 Instantiate(newGameObj, new Vector3(x, y, z), Quaternion.identity);
                 newGameObj.transform.parent = newParent;
             }

But getting this error :

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

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

Related Questions

How can I get Compound Colliders to work properly? 2 Answers

Instantiating an object to the location of child of another object. 2 Answers

instantiated child prefab invisible 3 Answers

Renaming Child Assets in Project 4 Answers

2d edge collider wont destroy? 0 Answers


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