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 ninjaboynaru · Oct 31, 2012 at 03:20 AM · instantiateprefabspawnparentchild

Give prefab a parent

I would like to make an instantiated object the child of the object it spawns on. The object it spawns on is an empty game object.

It keeps giving me an error "setting parent of a transform in a prefab is disabled to prevent data loss"

The ultimate goal is to have the object "polly" spawn on the empty game object as its child. When the empty game object has zero chilidren/poly has been destroyed another will spawn.

Thanks

 var polly : GameObject;
 function Start () {
 var ins : GameObject = Instantiate(polly, transform.position, transform.rotation);
 }
 function Update () {
 if(transform.childCount <= 0){
 Debug.Log("apple");
 polly.transform.parent = transform;
 }
 }


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
1
Best Answer

Answer by Montraydavis · Oct 31, 2012 at 04:06 AM

The problem is you're not actually assigning polly to the parent. Do it like so

 function AttachPolly ( )
 {
    var polly = Instantiate ( pollyObject, transform.position, transform.rotation  ) ;
 
    polly.transform.parent = parentObject ;
 }
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 ninjaboynaru · Nov 01, 2012 at 11:40 PM 0
Share

Worked perfectly

avatar image Montraydavis · Nov 01, 2012 at 11:50 PM 0
Share

Great. I am glad that it did. $$anonymous$$ind hitting thumbs up? ;) . Thanks for using Unity3D. It always $$anonymous$$ches me when I help others on here.

avatar image
0

Answer by Cosmology27 · Jan 27, 2020 at 03:11 AM

Sorry for the necro, but having this issue, and the code isn't working. I'm guessing "parentObject" is a variable you're assuming I'll predefine prior to that line. When I do this, I create a serialized field where I declare in the inspector what the gameobject will be, however, in this line of code, it's telling me I cannot implicitly convert a gameobject to a transform. In the line below, the "goCreate" is a gameobject I declared, the ShipPos is the position of the ship the laser is coming out of, and laserParent is a gameobject I defined before.

Also, is there a way to just make the object NOT have a parent? Currently my laser is parented to the ship, so when the ship moves, so do its lasers, which is obviously not right, lol.

             var laser = Instantiate(goCreate, ShipPos, Quaternion.identity);
             laser.transform.parent = laserParent;
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

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

11 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

Related Questions

Make a simple tree 1 Answer

Instantiating prefabs as child of a gameobject 1 Answer

spawning object as child of other:error 2 Answers

Prefab, procedural attachment of child, childCount return 0 1 Answer

spawn prefabs as children of another game object 2 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