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 KittyKatKat · Apr 12, 2014 at 06:22 PM · c#instantiateparentchild

Instantiating as a child error

 void BubbleCome() {
         Instantiate(bubble, new Vector3(transform.position.x + 1f, transform.position.y + 1.5f, transform.position.z + 1), Quaternion.Euler(new Vector3(38, 0, 0)));
         bubble.transform.parent = transform;
         Invoke("BubbleCome", Random.Range(repeatMin, repeatMax));
     }

The idea is that the continuously instantiated object (bubble) becomes a child of the game object to which the script is attached to pretty much as soon as it spawns. But the problem is that unity gives me an error:

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

Any idea why this happens and how I could fix it? Other than this function the only other one is the "BubbleCome();" set in the Start function.

Thanks! :)

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

1 Reply

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

Answer by nicolasjr · Apr 12, 2014 at 06:43 PM

I think you should create a GameObject pointer to instantiate it. such as:

 GameObject newBubble = (GameObject)GameObject.Instantiate(bubble, new Vector3(transform.position.x + 1f, transform.position.y + 1.5f, transform.position.z + 1), Quaternion.Euler(new Vector3(38, 0, 0)));

Then:

 newBubble.transform.parent = transform;
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 KittyKatKat · Apr 12, 2014 at 06:50 PM 0
Share

It works! Thank you so much! :)

If you don't $$anonymous$$d telling me why it should be like that? I mean, I understand that pointers point at a certain part of the memory, but simply why do we have to use them ins$$anonymous$$d of simply using the object (prefab) we just dragged into our script.

Of course, it's not obligated, it's just so I know what to do and why I'm doing something next time I try to do something similar (I hope that makes sense).

Again, thanks! :)

avatar image nicolasjr · Apr 12, 2014 at 06:54 PM 2
Share

Hey $$anonymous$$itty,

That happens because your prefab is not in the hierarchy, so, you're trying to change it's parent when, in truth, it doesn't have any. Does that makes sense?

You can only access parents of transforms in the scene, and, despite the fact that you instantiated a prefab in the scene, you still trying to change properties from the pointer you have from the prefab outside the scene!

Hope I could explain well enough!

avatar image KittyKatKat · Apr 12, 2014 at 07:11 PM 1
Share

Ah, so I'm trying to manipulate the prefab it self, but not the object inside the hierarchy. I didn't understand it at first, but I think I know what you mean! Thanks again, Nicolas!

avatar image nicolasjr · Apr 12, 2014 at 09:12 PM 0
Share

Exactly, $$anonymous$$itty! you got it right! Good thing I was able to explain it properly. :)

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

22 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 avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

Instantiated GameObject gets spawned as a child 2 Answers

Getting transform from an instantiated clone to parent it? 1 Answer

Parent object to child of another? 1 Answer

How to find and remove the last child of parent? 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