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 cr1t1cal · Aug 17, 2012 at 01:37 AM · prefabsruntimenested

Nested Prefabs at Runtime

I'm having an issue with nested prefabs currently.

This is the code causing the problem:

 if(GUI.Button(new Rect(20,20,80,20), "Buy Burstfire Rifle")) {
             GameObject burstfireRifle = (GameObject)Instantiate(burstfireRiflePrefab);
             burstfireRifle.transform.parent = playerObject.transform;
         }

It is tossing back the following error at runtime:

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

What it should be doing is taking a prefab for the burst fire rifle and making it a child of the player object (also a prefab, currently).

A few extra notes for reference:

The weapon prefabs are currently just an empty game object with a script attached that handles firing capabilities. I will probably need to make it both a script and a 3D model in the future, however, so I would think this needs to stay as a prefab rather than just adding a component script to the player prefab.

The player prefab is an empty game object containing all of the necessary scripts and physics objects and sounds with a single child containing the 3d Model (and animations). Doing this was required to fix animation errors.

I just need some what that I can ensure that whenever I move the player object, the weapon moves and rotates with it. If this can be done without parenting, that might work as well.

There will end up being hundreds or so equipment items in the end, so I would rather not place all of them on the prefab at start and enable them through the shop UI. There is also the possibility of multiple of the same item, meaning parenting at runtime is my current solution.

I've looked into the issue and it seems like nested prefabs is a wanted, but non-existant feature for Unity. I did see a few extensions that might solve the problem, but I would prefer not spending 50 or so bucks to find out there was another way to fix the issue.

I imagine having one instance of a prefab take on the orientation of another prefab is a common thing, so is there any way I can get around this limitation?

Willing to rework my idea, but I feel like there is something I must be overlooking here to make my current setup work.

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

Answer by DESTRUKTORR · Aug 17, 2012 at 02:48 AM

I believe what's happening here is that it's trying to edit the prefab, and not the instantiated object. Try using

 GameObject burstRifPref = GameObject.Find("burstfireRiflePrefab(clone)");

or just finding the instantiated object in the hierarchy and manually setting it as a child of something else. I believe it will (temporarily) break the prefab, but at runtime... it really doesn't matter all that much, since unless the player will be creating/editing prefabs, they shouldn't need to be linked.

But seriously... keep the questions simpler, ok? Try to keep it to one problem at a time, and we don't need the life story of your entire project... just what you're directly trying to do right now, and what has and hasn't worked for you thus far, that directly pertains to what you're asking about. Not trying to be mean or anything, just... that was kinda a wall of text... I'm a little surprised I actually read it all...

Comment
Add comment · Show 3 · 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 cr1t1cal · Aug 17, 2012 at 04:18 AM 0
Share

Thanks, I'll give that a shot.

Also, I provided all information that might have been necessary. I felt it was simple enough to begin with :O If the solution wasn't easy, it might have been necessary to know what did or did not need to stay a prefab. Not sure what I could have removed without people potentially asking questions or giving unusable answers. Also, I'd rather someone give me extra information than post a "How I do this give me code plox" question.

Anyway, thank you for the reply. I was unaware my solution might have been trying to reference the prefab itself and not the created instance. This should work for me.

avatar image cr1t1cal · Aug 17, 2012 at 04:50 AM 1
Share

Got it. It was almost as you described, but ins$$anonymous$$d of finding the rifle clone, I needed to find the instance of the player unit and Unity was able to understand that the clone I created was already referencing an instance.

The only code that I had to add was a small amount in the Start() function:

void Start () { playerObject = GameObject.Find ("PlayerUnit"); if(playerObject == null) { Debug.Log ("Player is null"); } }

And it works perfectly now. I may move this code into Update() in case I ever destroy the player (probably not), but for now this will do.

Again, thanks for sending me in the right direction.

avatar image DESTRUKTORR · Aug 17, 2012 at 06:12 PM 0
Share

No problem :) and don't take my criticisms too seriously XD I was just sayin' it's usually better to give what you need to give, first (like code and a brief explanation as to what's going wrong) and then expand on that, as needed. Good luck in the future! :D

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

10 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

Related Questions

So I know nested prefabs aren't supported.. 2 Answers

Saving Runtime Generated GameObjects to Prefabs 2 Answers

How can record the total amount of prefabs that are spawned during runtime? 0 Answers

Problems with nested prefabs 4 Answers

A Question about applying changes to nested prefabs 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