I have a sprite that appears and disappear in my UI from time to time. To do this, I'm simply modifying the sprite's "enabled" property:
player2Piece.enabled = enabled;
However, when I enable the piece, it would occasionally not appear, though the game object and sprite component were both active/enabled. I noticed that if I modify the sprites color, or if I suspend/resume the game, the sprite reappears.
So, digging into the UISprite/UIWidget/UIRect code, I found that only the UIRect is doing anything in OnEnable. For some reason, if I comment out the OnInit line, my sprites appear consistently every time and behave as expected:
// In UIRect code.
protected void OnEnable()
{
mAnchorsCached = false;
//if (mStarted) OnInit();
}
So, that seems to fix my issue...but I don't understand what that line does or why commenting it out causes the sprite to show as expected. When toggling Sprite visibility, is it incorrect to use the "enabled" property of the component? Is there a "right" way to do this that I'm not seeing?