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 /
  • Help Room /
avatar image
0
Question by chelder · Apr 15, 2016 at 05:28 PM · staticscenessingletonshared

static class disappear when reloading the same scene?

I have a static class:

 public static class SharedBetweenScenes {
     public static string value;
 }

In a script within a GameObject of the Scene A, I assign a value as follows: SharedBetweenScenes.value = "hi duby duby";

I load the Scene B. I can get the stored value doing: string value = SharedBetweenScenes.value;

Unfortunately, if a load the Scene A again and try to assign a different value: SharedBetweenScenes.value = "hello dude!"; the debugger says The name 'SharedBetweenScenes.value' does not exist in the current context.

Furthermore, if I load the Scene B, SharedBetweenScenes.value will have the old value of "hi duby duby" instead of the new value "hello dude!"

What is going on here? I also tried the singleton code proposed here with the exactly same problem.

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

Answer by TBruce · Apr 15, 2016 at 05:53 PM

@chelder Because SharedBetweenScenes is not a MonoBehaviour class which means you can not attach it to a GameObject means you are leaving something out in your question leaving for some guess work.

If SharedBetweenScenes was created as a MonoBehaviour class then you could do something like in the Awake() function

 DontDestroyOnLoad (transform.gameObject);

Since this does not seem to be the case you can look to do a few different things (you can try one or a combination of)

  1. You could try placing DontDestroyOnLoad within the class the uses that SharedBetweenScenes class.

  2. You could save and load your data using PlayerPrefs.

  3. Add [System.Serializable] above all class defines.

Comment
Add comment · Show 3 · 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 chelder · Apr 15, 2016 at 06:12 PM 0
Share

The third option did not work:

 [System.Serializable]
 public static class SharedBetweenScenes

I also tried adding:

 [SerializeField]
 public static string value;

I'm not sure what I'm doing when I wrote the above code, by the way.

The second option does not seem for me as the description says: "data is stored (persisted) on the device". I don't want to keep the data between executions of the game.

I can't do the first option as I don't have any Start function or constructor on it. The SharedBetweenScenes static class only stores static values.

A $$anonymous$$onoBehaviour class cannot be a static class. I have used a static class in order to access it anywhere without having to attach the script to a GameObject. It must be something I don't know about Unity, so it does not work.

avatar image TBruce chelder · Apr 15, 2016 at 07:04 PM 0
Share

@chelder I find your code working fine

 public static class SharedBetweenScenes
 {
     public static string value;
 }

To better help you I would need to know more. But after looking at your original question I see the code

 string value = SharedBetweenScenes.value;

"string value" is a temporary value and the only thing that will be static is SharedBetweenScenes.value. You need to access SharedBetweenScenes.value directly when reading and writing.

avatar image chelder TBruce · Apr 15, 2016 at 08:03 PM 0
Share

O$$anonymous$$G it was an if clause in a wrong place. Nothing related with the static class. So much time trying to figure out was going on, obsessed about the static stuff and the singleton stuff. Somehow, you allow me to unblock my $$anonymous$$d to find the silly problem. Thank you very much! I'm going to choose your answer as the correct one because I think it is a very good generic answer for anyone who search for a similar question. Thank you again! :)

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

55 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

Related Questions

Singleton Null Static Reference 1 Answer

Non-Static Member 1 Answer

global static function returns nullReference error [unityscript] 2 Answers

Setting objects that are large-scaled to static causing Unity to be stuck in '5/11 Clustering' 0 Answers

Object Reference not set to an instance of object / printing a variable from another scene 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