Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ShroomWasTaken · Jan 28, 2018 at 07:11 PM · prefabdatacopy

How to create a deep copy of a Prefab?

I'm having an issue where I have a component called Item which holds an ItemData variable in it, which represents the "internal state" of the item, including a reference to its prefab.
When I add a ItemData to an inventory (array), it copies the ItemData like this and then places the copy into the array/inventory:

     public ItemData(GameObject prefab, string name, string description)
     {
         this.prefab = prefab;
         this.name = name;
         this.description = description;
     }



And then the original Item GameObject is destroyed, however it seems that this.prefab = prefab doesn't actually copy it, because when i run it, the prefab variable in the copied ItemData is null. Which results in bad things when I try to try to instantiate it (which I for example do when I drop the item from the inventory)

So how would I actually copy the prefab and not reference it?

Comment
Add comment · Show 3
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 hexagonius · Jan 28, 2018 at 08:44 PM 0
Share

Depends. Do you want a copy of the prefab as another prefab, or as a temporary gameobject used during game time?

avatar image ShroomWasTaken hexagonius · Jan 28, 2018 at 08:58 PM 0
Share

I want a copy of the non-instantiated prefab.

avatar image hexagonius ShroomWasTaken · Jan 29, 2018 at 07:42 PM 0
Share

What's the exact use case of this? Because either you assign the differences between your items on instantiate, that's when it matters for the game, or you create a copy as a prefab for some other uses. The whole purpose of a prefab is being copied when needed, by instantiating one. What use do you have for that somewhat redundant copy?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by victorbisaev · Jan 28, 2018 at 09:38 PM

"GameObject" is a reference, so if you put a reference to the original Item into an array item and then remove the original Item, the reference becomes not non-valid. So either you should keep the original Item and use it as "Original" template for all instantiated items from your inventory, or the "prefab" field should reference a prefab from your Project (which is always available for instantiation).

Comment
Add comment · Show 2 · 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 ShroomWasTaken · Jan 29, 2018 at 04:08 PM 0
Share

The original GameObject is always there since it is a prefab and cannot be removed.
I think my problem happens when the ItemData gets copied and moved into the inventory array. I don't think it actually copies the GameObject variable in it, which points to the prefab, but actually references that variable. So when it is copied and the object which I copied from is destroyed, the copy's GameObject variable is no longer valid due to the fact that it was referencing the variable to the prefab and not the prefab itself.

I have no idea if that makes any sense at all, if not I think I might have to paste the actual code.

avatar image victorbisaev ShroomWasTaken · Jan 29, 2018 at 08:23 PM 0
Share

Ok, then maybe you could give more information on how "when the ItemData gets copied and moved into the inventory array" happens?

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

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

Related Questions

Instantiated coconuts not working 0 Answers

Load Data from EditorWindow to Prefab 0 Answers

Using Instantiate on a Rigidbody rather than a whole GameObject 1 Answer

Creating Prefab causes scripts to break, Copy/Paste Gameobject doesn't 0 Answers

Instatiate prefab at another prefab issue 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