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
4
Question by George · Oct 03, 2010 at 07:56 AM · serializationcustom-inspectorpropertiesfield

MonoBehaviour Properties not saving their values from Editor

When I change a value, it stays, but as soon as I hit play it resets to 0/default.

public class SomeComponent : MonoBehaviour
{
    public int SomeValue { get; set; }
}

Custom Editor:

[CustomEditor(typeof(SomeComponent))] public class SomeComponentEditor : Editor { private SomeComponent Target { get { return (SomeComponent)this.target; } }

 public override void OnInspectorGUI()
 {
     EditorGUIUtility.LookLikeInspector();

     EditorGUILayout.BeginVertical();

     Target.SomeValue = EditorGUILayout.IntField("Some Value", Target.SomeValue);

     EditorGUILayout.EndVertical();
 }

}

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

Answer by George · Oct 03, 2010 at 07:59 AM

Yes this is all a ruse, but the purpose was to note it down here for others.

What you have to do is not use auto-properties. You have to apply the [SerializeField] attribute to your backing field, so the following, modified version of SomeComponent works:

public class SomeComponent : MonoBehaviour { [SerializeField] private int _someValue;

 public int SomeValue
 {
     get { return _someValue; }
     set
     {
         if (_someValue == value) return;

         _someValue = value;
     }
 }

}

It's a shame, because auto-properties are so neat. Unity should definitely add [SerializeProperty], knowing that the compiler should make its own backing field.

Comment
Add comment · Show 2 · 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 Cyb3rManiak · Oct 19, 2010 at 01:21 PM 0
Share

You got me there! You naughty prankster you :) Yup, it's a good piece of information. +1 just for kicks.

avatar image Tom Norris · Dec 25, 2010 at 05:00 AM 0
Share

See, now this makes my day just a little rougher around the edges...I do NOT have to do this for public variables, they work just fine out of the box. On the other had I have this EXACT problem when using public static variables. You gave me a glimmer of hope for a moment there though, really thought this would fix my problem, but it doesn't seem to affect it. if you feel like it you can look at my question Static variables assigned in the inspector

avatar image
0

Answer by chirhotec · Feb 17, 2011 at 03:07 PM

I'm having this problem, but I don't understand how this fixes it. I've been using the custom ExposeProperty attribute provided here. I could have sworn it worked perfectly in the past, but at some point it broke and I can't figure out how to fix it.

The problem with the proposed solution is that when you made changes to _someValue from the editor, it doesn't call the setter, and so you never run the additional processing your setter might do.

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 Garth-Smith · Sep 04, 2013 at 10:45 PM 0
Share

$$anonymous$$aking changes to _someValue ins$$anonymous$$d of using SomeValue doesn't call the setter at runtime either. This is not specific to the Unity editor.

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

1 Person is following this question.

avatar image

Related Questions

Using DuplicateCommand and DeleteCommand with Properties 0 Answers

Custom Inspector data serialization in JavaScript 0 Answers

Property drawer stops variables from being read 0 Answers

Creating a SerializedProperty for type 1 Answer

Serializing object in scene? 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