NGUI Sprite not visible in 3d when added by code

When i add them by hand its nice two-sided GO with sprites, but when i do this by code

GameObject squadUnit = new GameObject("squadUnit"+i);
UISprite uiSpr = squadUnit.AddComponent<UISprite>();
uiSpr.atlas = atlas;
uiSpr.spriteName = "frame2";
uiSpr.panel = uiPanel;
squadUnit.transform.parent = panel.transform;
UISpriteAnimation uiSprAnim = squadUnit.AddComponent<UISpriteAnimation>();
uiSprAnim.framesPerSecond = 8;
uiSprAnim.namePrefix = "frame";

I see a rectangles but animation frames not visible
How to add them properly?

alt text

alt text

You’re probably having parenting issues with your widgets and panel.

Instead of having to worry about parenting, let NGUI do it for you, use NGUITools.AddChild:

GameObject squadUnit = NGUITools.AddChild(uiPanel.gameObject, new GameObject("squadUnit"+i)); // this will create the object and parent it properly
UISprite uiSpr = squadUnit.AddComponent<UISprite>();
uiSpr.atlas = atlas;
uiSpr.spriteName = "frame2";
//    uiSpr.panel = uiPanel;                          don't need that no more
//    squadUnit.transform.parent = panel.transform;   nor that

And btw, sometimes widgets don’t appear even if you add them manually in-scene, if that happens just pull your panel a little bit forward. See this for more info as to why this happens (it’s a Unity thing)

It’s probably not on the right layer to be rendered by the camera that is doing the NGUI stuff. That’s normally what it is when it happens to me :slight_smile: