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 Joakim · Sep 24, 2014 at 10:24 AM · editorprefabscenescene-loading

Handling scene objects

Salut !

Problem:

I have multiple unity scenes in my game. Each scene contains objects of various types, collectable items, bacgrounds, sprites etc. All this objects are statefull, meaning their attributes change, like, position is changing, sprite image to render etc.

Now, lets say I am on "scene1", I collect item, do all kind of things, I change the state of the whole scene. After that I leave "scene1", and go to "scene2".

Now, when I go back to "scene1" I want my scene1 to be in exactly the same state I left her, all the items in right positions with their attributes set...

Questions:

Do I basically want to create the whole scene from code every time it loads, because when I set up my scene through editor, each time the scene loads, it loads with all the objects in their originally defined editor states.

This leads to question: Should I even use editor to set up my scene objects ? Should I have all of my objects (ALL, including backgrounds and player) as prefabs so I can create all of them from code, avoiding the editor defined objects totally (for the release version) and using it only in development (to test things and similar).

Is there any way to make editor defined objects not to load in their original state but in their updated, actual state ? Because it is convenient to use editor to set up the scene nicely.

Are there any general good practices on this?

EDIT (Some more questions :P):

I could simply put DontDestroyOnLoad for all the objects and deactivate them when leaving scene :), and then activate them when I come back. Would this be an option for a smaller scale game with not so many objects ? Or this is absolute no ?

Should I serialize all objects on the scene so I can Instantiate them on scene load. Maybe I could use something like Memento pattern for that ? http://www.oodesign.com/memento-pattern.html

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

Answer by bradmarxmoosepi · Sep 24, 2014 at 11:37 AM

You could use the PlayerPrefs class for this.

Essentially, on Start for your item:

 void OnStart()
 {
   if (PlayerPrefs.GetInt("playerHasItem1") == 1)
   {
     // Do code for player has item
     Destroy(gameObject); // Don't show the item if the player has picked it up already
   }
   else if (PlayerPrefs.GetInt("playerHasItem1") == 0)
   {
     // Do code for when the player does not have the item
   }
 }

You can use similar code in other places for checking if the player has the item. It would be nice if the PlayerPrefs had a boolean storage function as well.

[EDIT]

For boolean storage in PlayerPrefs, have a look at BoolPrefs from the Unify Community Wiki.

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 Joakim · Sep 24, 2014 at 11:59 AM 0
Share

Thx for the answer, but I think PlayerPrefs is not designed to store so much scene related data. $$anonymous$$aybe it could be used to check which items player collected and similar, but no way I could put all the info for lets say 10 scenes, item positions and states for each scene, puzzle states, various stuff... I'm now looking into this memento pattern to build a system for storing and retrieving all that info for each scene

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

26 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

Related Questions

Open scene and load prefab 1 Answer

Cannot close/unload a scene that is open in editor during playmode (using C# code)? 3 Answers

Is it possible to ensure that certain game objects don't get saved in the scene or otherwise hook into the default save scene to run custom editor code? 2 Answers

Get the currently open scene name or file name 8 Answers

'Resource file has already been unloaded' error when exiting application in Editor 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