Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 HBishop · Jul 28, 2020 at 08:51 AM · singletonreferencingadditivestatic variable

Singleton and multiple scene data

Hi, I currently have a singleton in my project called GM.

I also have 3 scenes. I'm now using additive loading for the scenes. In scene 2, I have a manager script with a list that I'd like to access from the GM script (for saving purposes).

What would the best way to go for making this list accessable? I've heard many people say static is not good.

So far I've pinned ideas down to: make the manager script singleton, make it a static, or reference from the GM.

P.s. I don't need the whole script to be accessible, just that one list of bools which i will get from an array of type where my class is a data holder (it has a Gameobject, int and 2 bools).

Thanks for any help

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
2
Best Answer

Answer by xxmariofer · Jul 28, 2020 at 09:52 AM

first, making the list static it is totally fine but only when making the list static makes sense, is the list dependant of the instance of the manager itself? or you want that list to get shared acrross all manager instances? if you want the list to get shared is totally fine to make it static. using the singleton pattern is a good option, specially if there will be only one instance of the manager class and only one, but abusing of singletons might impact the code structure later in the project. i imagine GM stands for gamemanager and manager is other type of manager class, if thats the case i would not create a singleton for each manager class but only one singleton (the GM) and reference to those scripts from the GM

Comment
Add comment · Show 5 · 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 HBishop · Jul 28, 2020 at 11:36 AM 0
Share

Yes, the manager script will only have one instance. But I only need that whole script for that scene. It's only the list that needs to be used outside of the scene. So singleton would still be ok? There's no performance issues for making it a singleton when only needing to access the list outside. The other methods and functions in the manager script are using things that are in the scene 2. Would having a singleton of this manager script cause errors because the scene 2 objects aren't in scene 1 for example but the manager with the scene 2 refs is.

Thanks

avatar image xxmariofer HBishop · Jul 28, 2020 at 12:01 PM 1
Share

if the manager script is only beig used in one scene i would discard singleton, if the instance of the manager only appears in one scene but you need the list in all scenes go with the static approach

avatar image HBishop xxmariofer · Jul 30, 2020 at 11:42 AM 0
Share

Hi, With a static list, it wouldn't lose track or overwrite when I reload the same level. So if I change the static int list then go to scene 2, then change it again and then return to scene 1, it would have the last changed list? Would this also be the same if I had a static instance of the monobehaviour class. Static $$anonymous$$yClass instance=this; Without don't destroy on load, would it behave similarly? Thanks

Show more comments

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

131 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

Related Questions

Data from a simple singleton / static class return null 1 Answer

good way to get reference of transform of newly instantiated player object? 0 Answers

How does a singleton maintain a persistent value? 1 Answer

Saving last checkpoint hit when reloading scene - Singletons? 1 Answer

singleton, static var, local reference. Whats better? 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