• 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 -Jerome- · Apr 05, 2012 at 09:46 AM · prefabparent

parent a prefab

Hey guys I have another problem.

         Weapon = go.transform.GetComponent<ActualEquiped>().getEquipPlaces()[0].transform;
         
         pS = Resources.Load("ParticleSystems/BloodRushParticle") as GameObject;
         Transform toInstantiate = pS.transform;
         Transform Emitter = Weapon.FindChild("Emitter");
         instantiated = (Transform)Instantiate(toInstantiate,Emitter.position,Emitter.rotation);
         instantiated.parent = Emitter;

Every time I use this code I get this error:

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

I searched in internet for solutions and found something, but that didn't work.(Or I did something wrong)

Now I wanna ask how I can do that, to get no Data-Corruption.

Thx for 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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by clfischer · Apr 05, 2012 at 10:04 AM

Perhaps you could try working with GameObjects instead of Transforms.

 ...
 GameObject instantiated = GameObject.Instantiate(toInstantiate, Emitter.position, Emitter.rotation) as GameObject;
 instantiated.transform.parent = Emitter;
Comment
Add comment · Show 4 · 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 Bunny83 · Apr 05, 2012 at 10:56 AM 1
Share

This doesn't make sense. GameObjects doesn't have a parent. GameObjects have a Transform component and these components create the hierarchy. The GameObjects are just containers.

avatar image clfischer · Apr 05, 2012 at 11:02 AM 0
Share

You're right. I've edited the answer to take parent.transform ins$$anonymous$$d. Thanks.

$$anonymous$$y point was that he should instantiate a GameObject ins$$anonymous$$d of a Transform. This is the way I do it so I know it works. According to the documentation, you can also instantiate a Transform but perhaps you can't change its parent then, I haven't tried.

avatar image Bunny83 · Apr 05, 2012 at 11:35 AM 0
Share

No that doesn't make any difference. You can take any Component as source. Unity will always clone the whole GameObject with all components. It's just quite handy to use the component as source that you need after the instantiate. You can also use a customscript reference as source.

avatar image clfischer · Apr 05, 2012 at 12:31 PM 0
Share

O$$anonymous$$. In that case, your answer is probably correct. I'm impressed that you managed to guess the missing parts of the question :-)

avatar image
0

Answer by -Jerome- · Apr 05, 2012 at 12:38 PM

I tried this also. Same error

But thx for the fast answer!

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 clfischer · Apr 05, 2012 at 12:45 PM 1
Share

You should put that as a comment ins$$anonymous$$d of an answer, otherwise it's hard to tell which answer you're referring to :-)

Can you be more precise about what you're trying to do? Is @Bunny83 correct that you're actually writing an editor script and that go is actually a prefab?

avatar image
0

Answer by Bunny83 · Apr 05, 2012 at 10:54 AM

This can only happen when you:

  • execute this code in the editor (at edit time, not at tuntime)

  • and one of your two GameObjects are a prefab and not an instance

Since you instantiate the particle gameobject, i guess the Gameobject "go" is a prefab and not an instance.

If this is an editor script you should know that working with prefabs is a bit tricky. Prefabs are an editor only feature. Prefabs do not exist at runtime. Instantiate() will create a unconnected copy. If you want to instantiate a prefab like when you drag it into the scene you have to use PrefabUtility.InstantiatePrefab.

If you want to change the prefab (update it) you have to create a normal instance (Instantiate), do your changes and then use PrefabUtility.ReplacePrefab to copy the instance back to the prefab. After that you can delete your temp-instance.

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Accessing children of instances vs children of original prefab 1 Answer

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