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 zico_mahilary · Nov 01, 2012 at 12:23 PM · prefabspawnparentchild

spawn prefabs as children of another game object

i am making an endless runner where the player runs on top of a cylindrical - continuously rotating world. now i need to spawn obstacles (prefabs) on the cylinder and make them(prefabs) have the same motion as the cylinder... im done with the spawning but do not know how to place the spawned prefab as a child of another prefab(the level)so that they move along with the level. please help.

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

2 Replies

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

Answer by ZenithCode · Nov 01, 2012 at 01:27 PM

Ok so you need to set the parent object after you instantiate the new object as follows.

 Gameobject GO = Instantiate(myPrefab);
 
 myPrefab.trasnform.parent = ParentGameObject;

  • myPrefab is the one you want to instantiate

Comment
Add comment · Show 3 · 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 zico_mahilary · Nov 02, 2012 at 06:48 AM 0
Share

hi, thanks for the help.... i tried to use it but got this error :

"Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."

this is my code:

var brick : Transform;

function Start ()

{

Instantiate(brick, Vector3 (0, 0, 10), Quaternion.identity);

brick.transform.parent = transform.the_world;

}

pls help...

avatar image valyard · Nov 02, 2012 at 12:41 PM 0
Share

I don't know what transform.the_world is but this solution is wrong. With this code myPrefab.trasnform.parent = ParentGameObject; you are trying to set a GameObject where a link to a Transform is needed.

Plus, your code is totally wrong too. Look at my solution.

avatar image EpiFouloux · Jul 24, 2016 at 03:24 PM 3
Share

You're wrong, you should do

 Gameobject GO = Instantiate(myPrefab);
  
  GO.transform.parent = ParentGameObject;

You can't set a prefab parent, only a gameObject

avatar image
13

Answer by valyard · Nov 01, 2012 at 01:52 PM

To attach a GameObject as a child of another GameObject you need to set its transform.parent to that other GameObject's transform. For example if you want to attach a new prefab copy to this GameObject you would write something like this:

 GameObject obj = Instantiate(Prefab) as GameObject;
 obj.transform.parent = transform;

Update: Changed your code so it should work. Provided that you want the new brick copy to be attached to current GameObject.

 var brick : GameObject;
 function Start () {
   var object = Instantiate(brick, Vector3 (0, 0, 10), Quaternion.identity);
   object.transform.parent = 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 Ahaiziah · Jul 03, 2015 at 01:39 AM 0
Share

whoever you are, you ARE A$$anonymous$$AZING! been trying for like EVER to get it!

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

14 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

Related Questions

Make a simple tree 1 Answer

Give prefab a parent 2 Answers

spawning object as child of other:error 2 Answers

Child Animation synced with Parrent objects Animation 0 Answers

Instantiate a Prefab as child 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