This is a strange one.
I have a class, HudHandler, which holds some serializable fields & a serializable class (relevant code below):
class HudHandler
{
[System.Serializable]
class ReticleIcons
{
[SerializeField] public Sprite Aiming;
}
[SerializeField
] public ReticleIcons _ReticleIcons
= new _ReticleIcons
(); private UI2DSprite _Reticle;
void Awake()
{
GameObject obj
= new GameObject
("Reticle"); obj.transform.parent = _ReticleParent.transform;
_Reticle = NGUITools.AddChild<UI2DSprite>(obj);
_Reticle.sprite2D = _ReticleIcons.Aiming; // This looks like the culprit
}
//...
}
What appears to be happening (running through the debugger) is that UI2DSprite.sprite2D.set() is working fine, but somewhere before UI2DSprite.mainTexture.get() is called, the _Reticle's gameObject is corrupted.
In the editor, the _Reticle gameObject is missing both its UI2DSprite component and it's
transform component!
As a consequence, here is the run-time error:
MissingReferenceException: The object of type 'Sprite' has been destroyed but you are still trying to access it.
I've tried to fix it through various means, playing with public vs. private access, Awake() vs. Start(), but nothing seems to work.
Lastly, I
can fix this by simply taking the Sprite field out of the ReticleIcons class and into the HudHandler class. This isn't ideal, because I like to compartmentalize my fields through serializable classes for sanity's sake.
Any thoughts?