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 CirseiHyuga · Jan 26, 2017 at 08:46 AM · variableplaycustom editorresetserializefield

Variables in editor script reset when playing

Hello everyone ! I've written 2 little script in order to reproduce an issue I have. I've done many researches about it, but I didn't find the answers I was looking for, or maybe answers I didn't understand...

Here is the issue : I've a GameObject variable named 'gobj' in 'TestEditor' that resets to its default value when I press the play button. I ask for your help because I would like to understand why. Yet, I have serialized this variable, and I have use 'SetDirty' my Test script (what is the use of SetDirty anyway ?). I even tried to make it public and to do some stuff with the HideFlags.DontSave. Nothing works !

However, the variables in my 'Test' script don't reset, it is probably well serialized.

  • So, what difference beetween Monobehaviour inheritance and Editor inheritance justifies such an issue ?

  • I know I could put that variable in the 'Test' script (because srialization is OK in it), but the 'Test' script doesn't really need 'gobj' to do its job... So what is the best way to proceed ?

Hoping I've well exposed my problem, I thank you for every informations you could give me :)

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     public GameObject[] gameObjects= new GameObject[15];
 
 }

And the editor script :

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(Test))]
 public class TestEditor : Editor {
 
     [SerializeField]
     GameObject gobj;
 
     
     Test myScript;
 
     void OnEnable()
     {
         myScript = (Test)target;
     }
 
     public override void OnInspectorGUI()
     {
         gobj = (GameObject)EditorGUILayout.ObjectField("gobj", gobj, typeof(GameObject), false);
 
         for(int i = 0; i<myScript.gameObjects.Length; i++)
         {
             myScript.gameObjects[i] = gobj;
         }
 
         if (GUI.changed) { EditorUtility.SetDirty(target); }
     }
 }











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 steo · Jan 26, 2017 at 04:20 PM

There is a difference between MonoBehaviour and Editor scripts.

Object of MonoBehaviour can be attached to prefab and all its public fields and fields marked by SerializeFieldAttribute all will be saved into asset. And every time when Unity will load this prefab from asset, will be created object of your MonoBehaviour script and its fields will be filled by saved values.

But Editor is difference. Unity creates object of your Editor class every time when you open your MonoBehavoiur in Inspector. Editor can't be saved into asset. When your press Play button, Unity destroys old Editor object and creates new. This way you miss your gobj in inspector. Property drawers and EditorWindows are same kind.

Comment
Add comment · Show 3 · 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 CirseiHyuga · Jan 26, 2017 at 04:29 PM 0
Share

Ok ! Thank for those details ! Does that mean I've no other choice than declare the variables (intended to be filled in the inspector) in the $$anonymous$$onoBehaviour script ?

avatar image FortisVenaliter CirseiHyuga · Jan 26, 2017 at 06:15 PM 1
Share

Correct. Sometimes Unity will even delete the editor data when it simply saves or loads, so you definitely want to store relevant data in the target object and use that. In the built game, the editor scripts won't even exist, so that's part of the reason why their data is not persistent.

avatar image CirseiHyuga FortisVenaliter · Jan 26, 2017 at 06:18 PM 0
Share

Oh yeah... This is pretty logic explained like this. Thank you very much !

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Simple yet problematic] Array looses all the variables when I hit "Play" 2 Answers

Unity setting SerializedProperty.prefabOverride incorrectly 2 Answers

Create a Serializefield for Object Properties 1 Answer

Public Variables in MonoBehaviour Scripts reset to "None" in Playmode 0 Answers

How do I make serializefield/public readonly in the inspector, but so that I can still access variables in other scripts? 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