• 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 Cherno · Apr 15, 2013 at 02:28 AM · instantiateprefabparentcloneparenting

Parenting an instantiated Prefab... again

There seem to be a lot of question concerning this and while some of them seem to have working solutions, they do not work in my project no matter what I do.

An elevator platform should spawn a prefab model when function MakePillar is called. This model should then be parented to the platform. All code is included in the Elevator's script, so no third objects are involved.

 var PillarPrefab : GameObject; // The Pillar model that appears below the elevator platform. The function gets called in the Update function when a key is pressed.

 function MakePillar()
     {
         var Pillar : GameObject = Instantiate(PillarPrefab, transform.position, transform.rotation ) ;
         Pillar.transform.parent = transform ;
     }

What works: The first part, i.e. creating the pillar at the correct position. What doesn't work: the second part, i.e. parenting the new clone to it's parent, the elevator.

I already read and tested the code from the following threads...

http://answers.unity3d.com/questions/428578/parenting-an-instantiated-prefab.html

http://answers.unity3d.com/questions/55796/instantiate-a-prefab-to-a-parent.html

http://answers.unity3d.com/questions/428578/parenting-an-instantiated-prefab.html

Comment
Add comment · Show 4
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 Glister · Apr 15, 2013 at 04:44 AM 1
Share

The code:

 Pillar.transform.parent = transform ;

is trying to set the parent of your pillar to the transform of nothing. You will need to reference the elevator in order to get the correct transform. If the script is attached to the elevator gameObject then you should be able to set it with.

 Pillar.transform.parent = gameObject.transform ;

if the script is not attached to the elevator you will need to refrence the elevator gameObject with something like:

 Pillar.transform.parent = GameObject.find("Elevator").transform; 
avatar image Cherno · Apr 15, 2013 at 11:42 AM 1
Share

The script is attached to the elevator so the simple "transform" is enough for it to refer to the object it is attched to. Either way, it makes no difference if it's transform or gameObject.transform :(

avatar image Glister · Apr 15, 2013 at 04:45 PM 0
Share

how about:

 var Pillar : GameObject = GameObject.Instantiate(PillarPrefab, transform.position, transform.rotation ) ;
avatar image Cherno · Apr 15, 2013 at 06:12 PM 0
Share

It doesn't seem to change anything. However, something else in my script makes problems because the function itself works in an otherwise empty script (see below).

1 Reply

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

Answer by 1337GameDev · Apr 15, 2013 at 04:58 PM

Use debug statements to check if transform is null and such and print to the console. After your code try this: Debug.Log("parent is: " + Pillar.transform.parent.name);

If that gives a null reference, then the parent is not being saved. If so, then the parent is being applied but is being removed later. Make sure to run your instantiation code once or unity might create a ton of objects. Create a Boolean and just flip it when you run the code so it only runs once.

Comment
Add comment · Show 6 · 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 Cherno · Apr 15, 2013 at 05:39 PM 0
Share

Thanks for your comment.

I did everything you suggested and it gives back the parent name correctly in Debug.Log("parent is: " + Pillar.transform.parent.name);

I'm really not sure why this is not working and it's starting to become extremely annoying because now I have to find a workaround :(

avatar image 1337GameDev · Apr 15, 2013 at 06:03 PM 1
Share

If it prints it out, then something else must be changing it. Or the object (parent) is being destroyed. $$anonymous$$ake sure no other scripts are modifying this. Also make sure that you don't modify it anywhere else. If need be, create a test script and test the parenting in that script with all other scripts removed from all objects in a scene (or create a test scene that is empty).

avatar image 1337GameDev · Apr 15, 2013 at 06:05 PM 0
Share

Also if you have any null references (some could be silent) unity can act weird. Start with an empty scene with just a test script and 2 primitive objects from unity.

If that fails, then create a separate new project (with no 3rd party or custom assets imported) and try again. If that fails, then reinstall unity is your best bet.

avatar image Cherno · Apr 15, 2013 at 06:10 PM 0
Share

Heh, that actually works, now I'll check which pieces in the original code are making trouble and failing that, I'll just put it in a seperate script and cross-reference.

thanks!

Edit: After testing some more, I found out that the original script wouldn't work in any asset I already had in my scene, but if I create a new one in the editor and attach the script to it and effectively replicating my old elevator, it works like a charm. It's BS and it needlessly cost me several days but at least now it works. Thanks for all suggestions :)

avatar image Cherno · Apr 17, 2013 at 09:22 PM 0
Share

Days have passed and I finally figured out what caused the problem: Another script attached to the elevator that was supposed to parent objects on it and unparent them when they are no longer on the platform. Unfortunately, I sued the Detach.children command and thus, ALL children got detached :) Always try to check ALL scripts an object has, not just the one that seems to make problems :)

Show more comments

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

13 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

Related Questions

GameObject parenting during runtime 1 Answer

How to instantiate from prefab, not from instance? 3 Answers

Insantiate object to parent without changing scale? 0 Answers

Destroy 1 prefab object and changing the color/image 1 Answer

(Clone) from Instantiated Prefabs over Network 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