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
2
Question by slkjdfv · Apr 12, 2013 at 08:17 PM · editorstatic class

static class not keeping changes after playmode

When I'm editing my static class using an editor window it all works fine. But when I enter and exit play mode my changes are reverted. How can I fix this?

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

Answer by Bunny83 · Apr 12, 2013 at 08:36 PM

You can't "fix" this because the whole scripting environment is reset when you change playmode. Edit-time and run-time are two different worlds and static variables shouldn't be used as "permanent" memory because it isn't permanent. Static variables aren't serialized. Use non static variables of a serialized object like a MonoBehaviour or ScriptableObject-asset. If you just want to store values for your editor window, use EditorPrefs.

Comment
Add comment · Show 6 · 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 slkjdfv · Apr 12, 2013 at 08:43 PM 0
Share

What I want is the script to be able to save it's data without having to be attached to a gameobject.

avatar image Bunny83 · Apr 12, 2013 at 10:30 PM 1
Share

If it's an editor script and you only want to save simple data (int, string, bool) use EditorPrefs. If you have more complex data you might want to save your data as asset by using a ScriptableObject and the AssetDatabase class.

Anyway, if you "just" want to preserve the data of an editor window when you change between edit and runtime, just make your variables public and non-static. Editor classes are also serialized, but they aren't saved to disk

avatar image vargonian · May 10, 2014 at 08:41 PM 0
Share

@Bunny83 If you have a menu item that needs to manipulate a complex object, your latter solution won't work as far as I know, since menu items must be static, and statics can't access non-statics.

avatar image Bunny83 · May 11, 2014 at 03:11 AM 0
Share

@vargonian: I'm not sure what you're after. $$anonymous$$enuItems are implemented with static methods, yes, but it depends on what you want to do inside that method. You can always use GameObject.Find / FindObjectOfType / GetWindow / Resources.Load + Instantiate to get a reference to an instance of a serializable class.

You might want to implement a simple editor window since editorwindow instances have their public members serialized like $$anonymous$$onoBehaviours. Also you should be more specific what you mean by "complex". What kind of object do you talk about? Where is that object stored / referenced / used?

You probably should ask your own question with some of your code if you want any further help.

avatar image vargonian · May 11, 2014 at 03:48 AM 0
Share

I actually did ask my own question today: http://answers.unity3d.com/questions/705165/how-to-prevent-a-menu-operation-from-cancelling-wh.html

The object I don't want to die is not a Unity object and not a basic type. It is a really standard C# class, though. $$anonymous$$y menu item method creates an object which subscribes to the events published by that statically defined class, so if that object dies (on an editor reload), the menu item can't do its job because the event publisher will be destroyed.

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Request Help: Sprite Editor not Automatically Slicing Sprite Sheet 0 Answers

Editor Button Rotates Selected Object 1 Answer

Fatal Error While Playing -- VertexData.cpp 0 Answers

Too many bugs 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