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 darkpivotstudios · Jul 29, 2013 at 04:11 AM · c#parent

Trying to parent object to another object via script. "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption."

So I have a player object that instantiates a particle system that creates a muzzle flare. Everything is fine except I want the flare to be parented to the player. When I try this:

 public class Flare : MonoBehaviour {
     
     public GameObject Player;
     
     // Use this for initialization
     void Start () {
     
         transform.parent = Player.transform;
         
     }

I get this error:

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

I tried looking up the issue but I don't really understand the solutions that I came across. Can anyone explain a way to do this? Thanks.

Comment
Add comment · Show 6
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 Benproductions1 · Jul 29, 2013 at 04:38 AM 0
Share

You need an instance of your prefab to be able to set the parent. Right now, the prefab is not in your scene and therefore has no "parent". You need to "instantiate" (put it in the scene), in order to set the parent

avatar image robertbu · Jul 29, 2013 at 07:03 AM 0
Share

You need to do the parenting in the instantiated object, not the prefab. So your code will be something like:

 GameObject go = Instantiate(Player, somePosition, someRotation) as GameObject;
 transform.parent = go.transform;
avatar image darkpivotstudios · Jul 29, 2013 at 04:43 PM 0
Share

It worked perfectly, but only for one try. It was really odd, I just tested it, and it would instantiate and work properly, and then I ran the game again and now it won't instantiate. Everything is linked properly, I don't get any errors. This is the code:

EDIT: Never$$anonymous$$d, ignore my stupidity. It works, I accidentally turned off the emitter on the particle system! haha. Thank you Benproductions and robertbu.

avatar image darkpivotstudios · Jul 29, 2013 at 06:09 PM 0
Share

You put your comment in an answer so it seems I can't mark yours as the correct answer, oh well. I'll just select the other one. Thanks though.

avatar image robertbu · Jul 29, 2013 at 06:19 PM 0
Share

I didn't put my comment in as an answer because @D8ms was saying the same thing. I was just providing an example so that you would understand how to apply what he was saying.

Show more comments

1 Reply

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

Answer by D8ms · Jul 29, 2013 at 04:44 AM

Sounds like you are trying to change the parent of a prefab. Check to see that Player refers to an object that's located in your scene, not the a prefab.

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 darkpivotstudios · Jul 29, 2013 at 04:51 AM 0
Share

It is a prefab. I have it so when the shoot key is pressed, it instantiates a prefab of a muzzle flash particle system (which I want to parent to the player).

avatar image Benproductions1 · Jul 29, 2013 at 04:56 AM 0
Share

But to set the parent, it CANT be a prefab. I suggest looking at the examples in the documentation

avatar image darkpivotstudios · Jul 29, 2013 at 05:00 AM 0
Share

Okay, I think I just thought of another way I could do the effect.

Edit: Nope, still trying to figure this out. $$anonymous$$y player is a prefab and I can't figure out any way to parent something to it this way.

avatar image ThiagoVT · Sep 18, 2013 at 11:47 AM 0
Share

try use GameObject.Find("GA$$anonymous$$EOBJECT INSTANTIATED").transform.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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

"Center On Children" programmatically 1 Answer

Distribute terrain in zones 3 Answers

How can I listen for a function being called from a parent class? 1 Answer

Multiple Cars not working 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