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
2
Question by LukeZaz · Mar 23, 2015 at 10:39 AM · unity5editor-scriptingserializationvariables

Editor script variables resetting after restarting Unity

Most of it is in the title. I have prefabs with a specific script on them, and every time I restart Unity (doesn't happen when playing the scene) this one variable will reset to its default of 1.

Line for editing the variable in my custom editor:

 t.tilePriority = (short) EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to determine how this tile connects to others.  Negatives not recommended."), t.tilePriority);

Line defining the variable itself:

 public short tilePriority = 1;

EDIT: I've since added another GameObject variable and it won't save either. Both variables are defined in the main class, not the custom editor. More details:

GameObject variable defined:

 public GameObject backgroundTile;

Entire custom editor script:

 public class TileTexEditor : Editor {
 
     public override void OnInspectorGUI ()
     {
         TileTextureManager t = (TileTextureManager) target;
     
         t.tileType = EditorGUILayout.IntField (new GUIContent ("Tile type: ", "Tile type. used to identify this unique tile.."), t.tileType);
         t.tilePriority = EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to determine how this tile connects to others.  Negatives not recommended."), t.tilePriority);
     
         t.backgroundTile = EditorGUILayout.ObjectField ("Background Tile: ", t.backgroundTile, typeof (GameObject), false) as GameObject;
     
         serializedObject.Update ();
         SerializedProperty spr = serializedObject.FindProperty ("sprites");
     
         EditorGUI.BeginChangeCheck ();
         EditorGUILayout.PropertyField (spr, true);
     
         if (EditorGUI.EndChangeCheck ())
             serializedObject.ApplyModifiedProperties ();
         }
     }
 }

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

2 Replies

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

Answer by LukeZaz · Mar 27, 2015 at 01:16 AM

Found the solution thanks to user WazWaz on reddit.

Solution was to use EditorUtility.SetDirty in the custom editor.

Updated code:

 TileTextureManager t = (TileTextureManager) target;

 GUI.changed = false;
     
 t.tileType = EditorGUILayout.IntField (new GUIContent ("Tile type: ", "Tile type. used to identify this unique tile.."), t.tileType);
 t.tilePriority = EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to determine how this tile connects to others.  Negatives not recommended."), t.tilePriority);
     
 t.backgroundTile = EditorGUILayout.ObjectField ("Background Tile: ", t.backgroundTile, typeof (GameObject), false) as GameObject;
     
 if (GUI.changed)
     EditorUtility.SetDirty (t);
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
avatar image
1

Answer by Johat · Mar 23, 2015 at 11:19 AM

Sounds like a serialization problem to me. Is this variable defined in the MonoBehaviour or in the custom editor? I'm assuming the former from the syntax.

Anywho, Unity doesn't currently serialize shorts so the value will keep resetting. Changing the variable type to an int should fix your problem. A limitation of Unity, I'm afraid.

You can vote on this feature being supported in future versions of Unity here, but it doesn't look like it's coming any time soon.

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 LukeZaz · Mar 24, 2015 at 01:52 AM 0
Share

Didn't work, unfortunately.

public int tilePriority = 1;

t.tilePriority = EditorGUILayout.IntField (new GUIContent ("Tile priority: ", "Used to deter$$anonymous$$e how this tile connects to others. Negatives not recommended."), t.tilePriority);

No change. In fact, I've since added a GameObject variable and it won't save either. I've updated my main post with more information.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Filling list with editor scripts is empty on Play 1 Answer

Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers

How can I get complete path of a sprite? 1 Answer

When my scripts recompile or I enter play mode, my custom class gets reset 1 Answer

Dynamic serialized fields based on enum 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