• 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
3
Question by Smike · Apr 12, 2014 at 09:49 PM · prefabprefabsserializationcustom editorcustom-inspector

Using a custom editor script with a prefab instance causes my variables to reset on Play

I wrote an editor script for a Monobehaviour script of mine. Everything was going great until I put the GameObject containing the Monobehaviour into a prefab. At that point, anytime I tweak a variable in the prefab instance, it is automatically reset to the same value as in the prefab as soon as I hit Play.

Before Play:

alt text

After Play:

alt text

**Note that 5 is the default value in the prefab.*

Removing the Monobehaviour script from the instance and putting it back on fixes the problem. Replacing my Editor code by DrawDefaultInspector also fixes it.

Is there any way to fix it other than performing this ritual every time?

2.png (1.9 kB)
1.png (1.9 kB)
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

3 Replies

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

Answer by Smike · Apr 14, 2014 at 07:05 PM

Okay, I solved the problem! I should have communicated with my variable through a SerializedProperty instead of manipulating it directly. More info here:

http://docs.unity3d.com/Documentation/ScriptReference/Editor.html

Quote from the page: "it's advantageous to use the SerializedObject and SerializedProperty system to edit them, since this automatically handles multi-object editing, undo, and prefab overrides."

So I guess using SerializedObject and SerializedProperty is generally better! It seems like they're necessary to actually serialize my changes. I'll use them from now on :)

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 corris_unity · Nov 11, 2020 at 01:26 AM

Another solution is to call PrefabUtility.RecordPrefabInstancePropertyModifications after making your changes. This basically handles applying your changes as overrides, so that Unity knows not to revert the values in play mode.

I used it because I was in a situation where SerializedProperty was too limited for the changes I needed to make.

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
0

Answer by BHS · Apr 13, 2014 at 07:37 AM

If I'm correct this is because Unity will always get the value from the prefab regardless of the input made after. You must break your prefab connection in order to retrieve the value from it. If it's found at start you need to instantiate it at the start of the scene and get the value then.

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 Smike · Apr 14, 2014 at 05:16 AM 0
Share

Thanks, that does work as a temporary fix for the problem. I'll do that for now.

However, that is not how Unity usually is supposed to work, since if I do the exact same thing on an object that doesn't have a custom editor on, pressing Play doesn't revert it's values to the prefab's values.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Custom editor, serialize and prefab issue 0 Answers

Can I trust inspector? 1 Answer

ScriptableObject with Custom Editor resetting data in inspector 1 Answer

Variable modified with custom editor not saved when enter play mode, even with serialized attributes. 0 Answers

How to detect if Prefab was added to the scene in Edit Mode? 1 Answer


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