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 gamesbyangelina · Jul 16, 2013 at 02:07 PM · editoreditorwindowstatic

Does Unity overwrite static values on play?

I have a static variable in a class, let's call it StaticClass.ExternalValue. In the editor, I have a button that does the following:

 StaticClass.ExternalValue = "Some string! Hello."
 UnityEditor.EditorApplication.isPlaying = true;

In other words, it sets the value of the static field, and then starts the game playing. The problem is that the act of starting the game playing seems to cause the static fields to be overwritten with their default values again. How can I pass data from the editor to the game in this way?

Comment
Add comment · Show 4
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 PAEvenson · Jul 16, 2013 at 02:16 PM 0
Share

usually you need to mark things dirty when you change them via editor script to write the change to disc:

EditorUtility.SetDirty - http://docs.unity3d.com/Documentation/ScriptReference/EditorUtility.SetDirty.html

Not quite sure if can you can do that on a static class...

avatar image gamesbyangelina · Jul 16, 2013 at 02:20 PM 0
Share

This is making the change before the game is ever run though, no idea if that affects it. Will try this now, thanks.

avatar image PAEvenson · Jul 16, 2013 at 02:24 PM 0
Share

You may want to research ScriptableObject - http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.html

Its a good way to store data for runtime, like string data. It allows you to create an asset that holds whatever data you want and store it in the AssetDatabase. Do a quick google search for some more insight. Hope this helps...

avatar image gamesbyangelina · Jul 16, 2013 at 02:34 PM 0
Share

Definitely did help - great idea, will look into it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ashkan_gc · Jul 16, 2013 at 02:27 PM

As much as i know you have three ways to do that, 1- Use the EditorPrefs to store the value and read the value when playing in static constructor or Awake or somewhere else 2- Write the value to a file and read it back when you are playing 3- Put the values in a ScriptableObject and save them, then use the ScripableObject as an asset in your script.

The reason of overwriting is that unity simulates an executable running from scratch when you press play or go to play mode by code. You can not serialize static variables (not fully sure) so you can not tell the editor to save them.

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

17 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

Related Questions

Accessing GameObjects from Editor static class in Plugins...not. 1 Answer

Unity Editor Event System 1 Answer

Changes to Object made in custom Editor Window don't persist 0 Answers

Keep equal width for panels in EditorGUILayout.HorizontalScope 0 Answers

Execute editor window scripts when project errors are present 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