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 Legalt · Jan 30, 2018 at 04:27 PM · saveloadingsave dataload scenepersistence

Chest contents persistence between scenes

How do I keep chest contents persistent between changing scenes or loading game?

Lets say for example that in Scene1 I have chest with three items in it. Player interacts with chest and take out only second item. Then player moves to Scene2 and later returns to Scene1. After loading chest will return its content to default, so all three items will be there again. Same thing will happen when player close and load game again.

How do I make chest "remember" what was taken out? I guess I can record every chest GameObject or Script (...or only List<> of items inside of it) in save file but maybe there is some better method?

How do I handle this properly?

Comment
Add comment · Show 5
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 Harinezumi · Jan 30, 2018 at 04:34 PM 0
Share

No matter what you do, you will have to store somewhere the chest contents. You can do it by storing it in the level data, or in a level database. This also means that your chest contents need to be loaded from somewhere the first time as well (or generated, but that's almost the same thing).

avatar image Cherno · Jan 30, 2018 at 04:39 PM 0
Share

You could put a script onto any gameobject and add the [DontDestroyOnLoad] attribute. Have this script keep track of your chest contents, for example by having one variable for each chest (or in a collection of some sort). When loading a scene, you just find the chest and assign the contents based on your script (which is kept between scene changes).

Alternatively, as Harinezumi, save and load the data along with your level data via BinaryFormatter etc.

avatar image PersianKiller · Jan 30, 2018 at 04:41 PM 0
Share

you can use PlayerPrefs to save your stats ,if you go to another scene or even leave the game, your stats won't change this will save your stats in registry.

for example

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
 public int test;

     public void Start(){
          test = PlayerPrefs.GetInt ("test");
     test += 10;
     PlayerPrefs.SetInt ("test",test);
 }

so at the first time you play this script, it will return 10,second time 20, and ....

It's just an example to give you the idea.you can check if the player picked up items and if it was true the item should not be spawned in the chest :).

avatar image Commoble PersianKiller · Feb 01, 2018 at 06:14 PM 2
Share

It's generally not considered good practice to use PlayerPrefs to save game data (it's intended for options, configuration, preferences, etc). This official unity tutorial has a section showing how to use BinaryFormatter to save game data.

avatar image Harinezumi Commoble · Feb 01, 2018 at 09:21 PM 0
Share

Very true! PlayerPrefs are - as the name suggests - for configuration data, if you want to store anything more complicated then saving on disk is a better solution (although on mobile devices this becomes more tedious than with PlayerPrefs).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lensplaysgames · Sep 02, 2020 at 11:33 PM

Old question but I am in same boat... Bump

Comment
Add comment · 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

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

82 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

Related Questions

Loading error when using PlayerPrefs.GetInt with WebPlayer 2 Answers

can gameobjects be stored as static? 1 Answer

Saving lots of objects 1 Answer

Complicated save problem 2 Answers

my text size changes didnt saved by slider 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