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 /
avatar image
0
Question by David_Simoniants · Jul 22, 2018 at 05:53 PM · editorinspectorpropertycustomeditor

Property values changed by custom editor are reset when start playing

I have a scripts like this below. Problem is when I assign value to clampedValue in inspector and click play button all private fields are reset to their default value. So if I assign 60 to clampedValue, in play mode it becomes 50 again. How can I save values of properties (of private fields)?

(Sorry for my English)

 public class MyClass : MonoBehaviour
 {
     public float clampedValue
     {
         get { return _clampedValue; }
         set { _clampedValue = Mathf.Clamp(value, 0, Mathf.Infinity); }
     }
 
     private float _clampedValue = 50f;
 }


 [CustomEditor(typeof(MyClass))]
 public class MyClassEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         MyClass obj = (MyClass)target;
         obj.clampedValue = EditorGUILayout.FloatField("Clamped Value", obj.clampedValue);
     }
 }


Comment
Add comment · Show 2
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 Harinezumi · Jul 24, 2018 at 07:23 AM 0
Share

A different approach is to use serializedObject and access your variable through SerializedProperty variables, and then at the end call serializedObject.Apply$$anonymous$$odifiedProperties (); to "save" changes to the object you are editing.

There is a good example how to do this here, in the scripting reference.

avatar image Privateer Harinezumi · Jul 24, 2018 at 07:41 AM 0
Share

That's going into Property Drawers and Custom Inspectors. That's unnecessary in this case.

2 Replies

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

Answer by Sabre-Runner · Jul 22, 2018 at 06:39 PM

Maybe it's because you're handling a property and the actual field is not an inspector field?

Comment
Add comment · Show 5 · 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 David_Simoniants · Jul 22, 2018 at 06:48 PM 0
Share

In inspector debug mode I see private field is changed after changing property in inspector but when I click play button, $$anonymous$$onoBehaviour constructor is called and it resets the value of the field. How can i prevent it?

avatar image David_Simoniants · Jul 22, 2018 at 06:57 PM 0
Share

I tested something right now. I made my field public and changed the value of property. When I clicked play button it didn't change. So if saying 'inspector field' you mean public field then you're right but how can I set private fields as inspector fields.

avatar image Privateer David_Simoniants · Jul 23, 2018 at 09:34 AM 0
Share

An inspector field is any field that is exposed to the Inspector. If it is, the Inspector setting overrides the class construction. If not, then it doesn't.

You have two options to set an inspector field: mark it public (but than every outside class can access it) or use the [SerializeField] attribute on a private field.

avatar image David_Simoniants Privateer · Jul 24, 2018 at 06:32 AM 0
Share

Thanks, I used [SerializeField]

Show more comments
avatar image
3

Answer by sfilo · Oct 12, 2020 at 03:46 PM

I had a similar problem. It turned out I was editing a prefab and had to set EditorUtility.SetDirty(target); to indicate to the editor that the prefab instance has changed properties.

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 gfaramaz · Oct 14, 2020 at 03:37 PM 0
Share

I have been struggling on this for hours! Thank you so much!

avatar image StCost · Sep 01, 2021 at 03:15 PM 0
Share

Stuggled for weeks because some important variable was resetting. This single line of code fixed it

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

123 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 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 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

CustomEditor and inherited components 1 Answer

Change variable in inspector depending on enum using struct 0 Answers

Custom Inspector Layer Mask variable 2 Answers

How to assign a field within a PropertyDrawer? 1 Answer

expose public property (not variable) in inspector 9 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