ANSWER for your editor script reseting all variables when entering play mode.

Hello Unity Community,

I started a few months ago with editor scripting to make some processes a bit faster and easier to use for others it always worked fine till a week ago I suddently got some strange problems causing the script to set all its variables of its normal script it is overriding to being reset to its old state and not to its state I had given it.

Description of my problem to compare it to your own :
I have a variable “buttoncount” with default 0.

  • public int buttoncount = 0;

In my editor inspector I change it with EditorGUILayout.IntField to 5.

  • scriptTarget.buttoncount = EditorGUILayout.IntField("Button count : ", scriptTarget.buttoncount);

When I unfocus the IntField it still says 5, so you would think it saved its value, but that isn’t true when I enter play mode to test this it resets to 0 (old state).

Sometimes it was 5 when I wanted that, but when I changed it again to an other number for example 8. It was not resetting to 0 when I entered play mode, but to 5 (its old value).

Now I knew Unity was not setting everything to its default, but it was a saving problem. I’ve checked all over the internet and checked lots of forum post and questions on the answers site, but not 1 answer to their questions worked for me and a lot didn’t for the person who asked the question.

I first thought it was a problem that my public variables where not seen as serializeble variables (variables that can be changed and will be saved as it default when application is started) privates cant be set and wont save if editor would change them. You can force to save privates with [SerializeField] but my problem was that I had publics and those variables have this as standard. I tried everything to get them saved with gets and sets, [SerializeField], [System.Serializeble] for the classes and all options people told on forums. I placed next function in my script, but that also didn’t work.

if(GUI.changed){
    EditorUtility.SetDirty(scriptTarget);
}

Answer to this problem :
Well first I have to tell it has nothing to do with your code if you have exactly the same as what I have. I had made a editor to change objects in my hierarchy and see those changes in my game view. I needed to be in my screen as always, but it was not a editor window, but an inspector so when I switched to other objects It would disappear out of my screen. I solved this little problem by making a second inspector and lock the object where my editor was attached on so it stayed on screen while I was switching objects.

The problem in this is that you lock your editor script. I first didn’t know why, but I think I’ve found out ( can’t really tell because I cant look in the code how its exactly handling scripts in an inspector) but telling from my tests with debugs it disables and enables the editor script every time you change a variable in an EditorGUI control like an IntField and while it is disabled it can save changes to its target. When you lock your script you tell it that it may not close in any case, so it can never send its changes to its target script where your variables are stored.

When I lock the object I get this problem and when I unlock the object the problem is gone immediately. I hope this helps for a lot of people with this problem especially because editor scripts are made to make things easier, but with this problem it only makes things more difficult to use for the end user and developer of course.

I tested this on Mac and Windows because I first thought I only had it on my IMac, after a while it also occurred on my windows (didn’t lock the object immediately when I copied the project to the windows pc). My unity version is 3.5.6 I don’t know but I hope this will be solved in Unity4. Have not installed it yet but I will update this when I have, want to finish my current project first before I will switch.

Example script :
test.cs

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {
	public int buttoncount = 0;
}

testEditor.cs

using UnityEngine;
using System.Collections;
using UnityEditor;
using System;

[CustomEditor(typeof(test))]
public class testEditor : MonoBehaviour {
	public test scriptTarget;
	
	public void Awake(){
		scriptTarget = (test) target;
	}
	
	public override void OnInspectorGUI(){
		scriptTarget.buttoncount = EditorGUILayout.IntField("Button count : ",scriptTarget.buttoncount);
	}
}

Last words :
Well hope it helped and I can finally start entering my few hundred variables in lists and don’t have to save->quit-> restart every time it didn’t save anymore. Hope this explenation is posted on the right place and isn’t to long :slight_smile: I’ve seen lots of people with this problem so please tell them when you see someone with his question unanswerd, because this is a hell of a problem that is not documented anywhere what I know off and took me 4 days to find it out.

Best regards,

Ruben,

1 Like

Thank you. This is very nice way to use custom inspector. I used [Serializeble] from this topic: http://forum.unity3d.com/threads/130889-Custom-Editor-losing-settings-on-Play but it is not very convenient way.

I have this exact same problem with my custom editors. It works perfectly when the inspector is in its normal state, but when you lock it, it no longer save the data (edit data, press play and you lost it all).

I’ve also found out that while you have a locked inspector, your custom inspector code will not handle null cases well, for example:

CustomComponent myComp = (CustomComponent)target;
if (myComp != null){
...
}

Will say that your target is null even if target isn’t! (I’ve checked using the debugger, and if you delete the null check you can even use the myComp object without problem).

Does anyone knows a solution? It is very annoying not being able to use the lock because of this.

For now the only solution is to stop using target and instead do all your custom editors with SerializedProperty and calling ApplyModifiedProperties at the end. Example:

public override void OnInspectorGUI() {
  SerializedProperty myProperty = serializedObject.FindProperty("name");
  EditorGUILayout.PropertyField(myProperty);

  serializedObject.ApplyModifiedProperties();
}
2 Likes

It’s unbelievable this issue is still a thing. I face it on…

1 Like

You didn’t really need to necro a 10 year old thread.

Particularly when this only really happens due to user error or incorrectly written editor tools. It’s not an ‘issue’, it just happens when you do something wrong.

4 Likes

As post above cover it.

Closed.