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
1
Question by Helldubs · Nov 07, 2020 at 03:29 PM · instantiateprefabsave datapersistence

Make changes in prefabs persistent.

Lets say I have a prefab. I can instantiate it with the press of a button. I can do stuff to it, for example change a bool. Now, when it's destroyed and I want to instantiate it again (during the same session), it gets instantiated with its original values. How can I make changes persistent within a session?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by plamusse42 · Nov 07, 2020 at 04:33 PM

Hey @Helldubs !

I don't know what is the best practice but I came up with this :

You can use an other prefab as a prefab manager.

 public class PrefabManager : MonoBehaviour
 {
     public Color color;
     public GameObject square;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.A))
         {
             Instantiate(square, transform);
         }
     }
 }

AND

 public class Square : MonoBehaviour
 {
     public GameObject manager;
     // Start is called before the first frame update
     void Start()
     {
         Color managerColor = manager.GetComponent<PrefabManager>().color;
         GetComponent<SpriteRenderer>().color = new Color(managerColor.r,
             managerColor.g, managerColor.b);
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }

The trick is to drag the prefab from your project window in the GameObject public variable in both the PrefabManager and in the Square prefab (BTW this one could be anything else).

So to summarize : 2 Prefabs with 2 differents Scripts and give each the other prefab.

Comment
Add comment · Show 1 · 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 Helldubs · Nov 07, 2020 at 08:42 PM 0
Share

Thanks for the suggestion. This will definitely work, but it seems kinda taxing to switch around prefabs all the time. Especially if the program gets bigger in scale and a lot more prefabs needs to be switched and tracked.

avatar image
1

Answer by CodesCove · Nov 07, 2020 at 07:38 PM

I suggest you to use ScriptableObject to store the data. You can reference the prefabs from there and build & attach component that registers the GameObject reference to the scriptable object in the Start. That way you can have persistent data in the asset file even if you destroy the referenced gameobject and instantiate it again..

There is some learning to do how the SO works and how to best utilize it. I have mostly positive experience with this and it solves and makes a lots of data persistence (also save / load) + object reference stuff easier.

Check this out first: https://unity.com/how-to/architect-game-code-scriptable-objects

Comment
Add comment · Show 1 · 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 Helldubs · Nov 07, 2020 at 08:43 PM 0
Share

Will definitely tinker around with Scriptable Objects next time. In the meantime, I fixed my issue with a simple save and load class. But then again, this feels kinda overkill for what I really needed. Also, it wastes disk space... $$anonymous$$aybe it's time to get into ScriptableObjects ~ Thanks for the point in the right direction.

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

202 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 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 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

Prefabs dont instantiate directly on point 0 Answers

Referencing gameObject from script after Instantiate 0 Answers

Prefab of a whole scene? 3 Answers

Dynamical prefab, how to instantiate + positioning 1 Answer

Infinity runner - best way to spawn board in runtime. 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