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 ThePeat · May 27, 2016 at 09:43 PM · c#prefabsreferencecopy

[SOLVED] Prefab as copy not as reference

Hey there,

currently I am working on a "space invaders" clone. I have just implemented a LevelSystem which allows the player to distribute points in stats to make the player/ship more powerful.

Whenever I make changes to the player they are also done in the actual prefab. To clarify the problem, let me describe it with more detail:

In instance my player has the property "Shots per second", which is actually 5. The ship is having a level up and I decide to spend 5 points in it so it is having a value of 6. Now after ending the game, the prefab'c ShotsPerSecond value is also 6. But I actually expected that the prefab's ShotsPerSecond value is staying 5 and is not touched by changes.

How can I avoid this? I do not want to store a reference of the prefab but I want a copy of it. I was already thinking of instantiating the prefab hidden in the main scene and then keep reference to this.

If this is not clear enough, I will add some code and screenshots.

Greetings

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 Kryzarel · May 27, 2016 at 10:11 PM 2
Share

From your description I would say you were on the right track about the instantiate. If you are keeping a reference to the prefab and altering that directly ins$$anonymous$$d of using Instantiate and altering the copy it creates, then the behavior you are describing makes sense.

avatar image ThePeat Kryzarel · May 28, 2016 at 02:34 AM 0
Share

Ultimately this was the way I implemented it. And it is actually working fine but I am still wondering if there are other ways to do it.

avatar image Kryzarel ThePeat · May 28, 2016 at 12:15 PM 0
Share

As far as I know there are only two ways to do it:

Drag and drop the prefab from your project folders to the Scene Hierarchy and keep a reference to that one, ins$$anonymous$$d of the original. Then proceed as normal and change its properties as before.

OR

In your Start() function (or wherever it's appropriate, really), call Instantiate(originalPrefabReference) save that in a variable and change that one.

Edit: I posted this and what's been said in other comments as an answer since this seems to solve your question I believe :)

avatar image RavenOfCode · May 28, 2016 at 04:17 AM 1
Share

This shouldn't be happening, any edits in play mode should have no effect on the prefab once you exit play mode.

avatar image Alec-Slayden RavenOfCode · May 28, 2016 at 05:04 AM 1
Share

If they're referencing a prefab in their inspector and not an instantiated object, and making changes to that prefab in play mode, the changes will persist (because they are editing an object in their project, not something in the hierarchy undergoing serialization)

avatar image vintar · May 28, 2016 at 11:57 AM 0
Share

As Alec Slayden said, the changes will persist if you change a reference to a prefab in your project. Instantiate is the only way to go and is for the exact purpose of not editing the original.

1 Reply

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

Answer by Kryzarel · May 28, 2016 at 01:06 PM

This is just a compilation of everything that I and others already stated in the comments, but I'm posting it as an answer so the question can be marked as solved.

The Problem

Since you are keeping a reference to the original prefab and not a copy, changes that you make to it in play mode will persist. Now you might be thinking:

"But changes made in play mode shouldn't persist after leaving play mode"

Yes, but not entirely true. The more accurate way to say it is:

"Changes made in play mode to objects in the scene hierarchy, don't persist after leaving play mode"

Since the object being changed was the original prefab (which is not in the scene), then changes would persist. In order to solve this you must change a copy of that prefab.


Solution

As far as I know there are only two ways to do it:

Drag and drop the prefab from your project folders to the Scene Hierarchy and keep a reference to that one, instead of the original. Then proceed as normal and change its properties as before.

OR

In your Start() function (or wherever it's appropriate, really), call Instantiate(originalPrefabReference), save that in a variable and change that one.

In this case I would use the first option. Since you only really need one copy of that prefab for the entirety of your game. The second option is better when you need to instantiate multiple copies of the same prefab during run time.

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 ThePeat · May 28, 2016 at 01:52 PM 2
Share

I actually chose the second option first, but now I am trying the other solution too. Both solutions are fine. Thank you!

avatar image Kryzarel ThePeat · May 28, 2016 at 02:05 PM 0
Share

Happy to help :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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Prefab, referencing them, using their methods:bug or intentional? 1 Answer

Distribute terrain in zones 3 Answers

How do I update the position of different prefabs to the current position? 0 Answers

Need suggestion to handle references in Singleton. 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