- Home /
[Simple yet problematic] Array looses all the variables when I hit "Play"
Hello, dear Community! I need your help :'<
I need to set up an array through a custom Editor; I store array objects inside of this array. However, the base array looses all inside objects once I hit "play". This can be observed using EditorGUILayout.IntField that I use in OnInspectorGUI() function
How can this be solved?
I am using two scripts,
Thank you very much!
//ArrayScript.js
#pragma strict
var branchArray : Array = new Array();
//ArrayScriptEditor.js
#pragma strict
@CustomEditor(ArrayScript)
class ArrayScriptEditor extends Editor{
var _TaAS : ArrayScript; //(target as ArrayScript shortcut)
function OnEnable(){
_TaAS = (target as ArrayScript); //set up the shortut
}
function OnInspectorGUI(){
GUILayout.BeginHorizontal();
GUILayout.Label("# of cells in branchArray");
EditorGUILayout.IntField(_TaAS.branchArray.length, GUILayout.Width(40));
GUILayout.EndHorizontal();
if(GUILayout.Button("push an array into the branchArray")){
var branch : Array = new Array();
_TaAS.branchArray.Push(branch); //I am pushing an array object into the other array
}
}
}
This seems like a serialization issue. Read http://blogs.unity3d.com/2012/10/25/unity-serialization/ and see if you get any ideas. I also have a feeling a list of lists might not be serializable. You could try using a different, more easily serializable, data structure.
Answer by AntiLunchBox · Jul 19, 2013 at 06:33 PM
This is most definitely a serialization issue.
Check out the link.
Also, you want to have something like this in OnEnable(): (this is in c# so you have to convert to js)
mObject = new SerializedObject(target);
And in your OnInspectorGUI() have:
mObject.Update();
in the beginning and
mObject.ApplyModifiedProperties();
at the end.
I haven't done editor scripts in js, but it should be about the same concept.
The article is exactly what I was looking for, thanks so much!
Answer by Owen-Reynolds · Jul 18, 2013 at 01:20 AM
Wouldn't branchArray = new Array();
destroy whatever was in it, when run? A regular script with a public-filled-in array just uses the array -- no initialization needed. I'd think this would be the same way. Get rid of that line.
Still not working :<
my ArrayScript is now
//ArrayScript.js
#pragma strict
var initialized : boolean = false;
var branchArray : Array;
and for the editor I added a little check OnEnable, so it looks like this:
//taken from ArrayScriptEditor
function OnEnable(){
_TaAS = (target as ArrayScript); //set up the shortcut
if(_TaAS.initialized == false){
_TaAS.initialized = true;
_TaAS.branchArray = new Array();
}
}
Hitting play still erases everything, although I initialize the Array only from the editor and only once
That looks a lot what worked for me (last seen over a year ago.) Except I used if(branchArray==null)
ins$$anonymous$$d of the extra bool (using a C# [] array.)
I'm wondering whether the editor script is somehow making and displaying a "ghost" branchArray. You wouldn't know, since the only way to check uses the editorScript. Also, I avoid javascript, so JS style new Array();
could require some tricks which I'm completely ignorant of.
Your answer
Follow this Question
Related Questions
Custom editor window resets array 1 Answer
How can I make variables reset when leaving play mode? 3 Answers
Variables in editor script reset when playing 1 Answer
Is there a Function Callback for when a vairable is altered in the Inspector? 1 Answer
Inspector variables from class in List are always initialized to its default values 1 Answer