- Home /
Inspector Scripts stops working after each compile
I have a custom inspector script for a spline, it stores points in a list. It fully works, but when unity recompiles any script in the project, all the data seems to get lost and the script doesn't work anymore (the contents in the inspector are gone) untill I reset the script in unity.
What can cause this problem? And how do I solve it? Because its very useless now, with each compile of a script I have to reset the spline script and redo all the work.
Update:
When saved in a prefab the data is there, even after a recompile. When I reload the scene or press play, the data is back visible in the inspector.
Is there maybe a way to catch a recompile event and refresh the inspector or something like that, or a manual deserialize?
Answer by SirIntruder · Sep 01, 2013 at 12:59 PM
You should stay away from saving permanent data into custom inspector classes. Save them in the inspected class instead. I'm not 100% sure on the life cycle of instances of Editor-derived scripts, but it is entirely possible that they all get deleted and recreated on recompile, together with anything that was statically initialized (all static classes' constructors are re-called on recompile).
For your second question - I don't know for a documented way of intercepting recompile events, but you could use the fact that static constructors are called on recompile. The catch is that most of the Unity functionality apparently doesn't work when called from static class constructors. It probably has something to do with the order of initialization of various codes on program-load. You can circumvent that by just setting a flag in static constructor.
public static class RecompileListener
{
private static bool loaded;
// This should ban setting Loaded to false outside the class.
public static bool Loaded
{
get { return loaded; }
set { if (value != false) loaded = value; }
}
static RecompileListener()
{
// This is called on code recompile
loaded = false;
}
}
//some Unity class method
if (!RecompileListener.Loaded)
{
RefereshStuff;
RecompileListener.Loaded = true;
}
Answer by Psymon · Dec 05, 2012 at 11:22 AM
If the data stored in your spline is not serializable, the inspector will not save the contents.
You may have to define other classes in order to make your spline serializable.
For example an array of an array is not serializable, you have to make another class in order to make it serializable :
[System.Serializable]
public class AssetArray {
public string[] array;
}
public class myClass {
public AssetArray[] listAssets;
}
If your prefab is saved as a file text you can check if the data is stored by opening it with a text editor.
The data is stored. When I close the scene and reopen it, the spline still exists. But when it recompiles a script the spline disappears. And when I close Unity and re-open it, it is still there.
This is exactly the same problem i encountered in my example. The inspector still showed me the content but when i compiled it disappears. Can you check the prefab as a text file to be exactly sure the data is stored in the file ?
Don't know what you mean by check as a text file. But when I open it with notepad I get: x ( 3.5.0f5 þÿÿÿ é Prefab Base ÿÿÿÿ € UInt32 m_ObjectHideFlags Prefab$$anonymous$$odification m_$$anonymous$$odification ÿÿÿÿ € PPtr m_TransformParent SInt32 m_FileID SInt32 m_PathID vector m_$$anonymous$$odifications ÿÿÿÿ € Array Array ÿÿÿÿ € SInt32 size Property$$anonymous$$odification data ÿÿÿÿ € PPtr target SInt32 m_FileID SInt32 m_PathID string propertyPath ÿÿÿÿ € Array Array ÿÿÿÿ @ SInt32 size char data string value ÿÿÿÿ € Array Array ÿÿÿÿ @ SInt32 size char data PPtr objectReference SInt32 m_FileID SInt32 m_PathID vector m_RemovedComponents ÿÿÿÿ Array Array ÿÿÿÿ SInt32 size PPtr
On the toolbar : Edit/Project Settings/Editor check if the asset serialization is set as Force Text like this :
http://uppix.net/f/0/3/3a69a998b3e25506a4f494c958974.jpg
Then when you open the prefab with Notepad try to find your script Spline in the text(there must be a line written $$anonymous$$onoBehaviour: ). When you find your script check if your variables are written.
For example :
$$anonymous$$onoBehaviour:
playAnimOnStart: 1
$$anonymous$$y monobehaviour has a variable named playAnimOnStart set to 1 in this prefab.
Done that, and the data is in the text file. m_Name: _name: Test _controlPoints: - _position: {x: .523728788, y: .0570619702, z: .113889799} ...
Your answer
Follow this Question
Related Questions
Material.mainTexture not working outside of runtime? 1 Answer
How do I get an array of names of all the sprites in a project, with an editor script? 2 Answers
Unity API for checking if an object was modified 3 Answers
How to minimize the main editor Window by script 1 Answer
Why would editor script be slow on first compilation? 0 Answers