- Home /
Question by
Jianyong_Luo_2014 · Jun 15, 2016 at 03:21 PM ·
texture2dmissingreferenceexceptionhideflags
Texture2D variable causes MissingReferenceException
I've suffered a MissReferenceException when using Texture2D. With below code,
public abstract class TestBase
{
protected readonly Texture2D texture;
public TestBase()
{
texture = new Texture2D(100, 100, TextureFormat.RGB24, false);
}
public abstract void UseTexture();
}
public class Test : TestBase
{
public Test () : base()
{
}
public override void UseTexture()
{
// others
texture.Resize(80, 80);
// others code
}
}
public class TestWindow : EditorWindow
{
private static TestBase base;
[MenuItem("Tools/Test")]
private static void Init()
{
// Initialization
}
public void OnGUI()
{
if (base == null)
{
base = new Test();
}
base.UseTexture();
}
public void OnDestroy()
{
// others
Resources.UnloadUnusedAssets;
}
}
we can get a Editor Window. With the steps below, I can get MissingReferenceException, and the "texture" will be "null". 1. The "TestWindow" will be opened automatically at Play Mode; 2. Exit Play Mode, the "TestWindow" will be closed automatically; 3. Open the window from the Menu, then the Exception occurs.
Someone told me that set the hideFlags of Texture2D to be "DontSave", such as:
public abstract class TestBase
{
...
public TestBase()
{
texture = new Texture2D(100, 100, TextureFormat.RGB24, false)
{
hideFlags = HideFlags.DontSave
};
}
...
}
The error is fixed. But I cannot actually get why.
Could someone explain it?
Thanks
Comment