Author Topic: UIRect.parent not initialized properly when using NGUITools.AddChild  (Read 7246 times)

wizardsmoke

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 5
  • Posts: 40
    • View Profile
I have come across an issue that seems to be a bug with UIRect and NGUITools.AddChild.

After using NGUITools.AddChild(parent, prefab) to instantiate a UIRect as a child of a panel, the UIRect does not find the parent panel (or possibly finds a different panel above the parent in the hierarchy), with the end result being that the instantiated UIRect is not drawn by the correct panel.  This breaks TweenAlpha functionality, since the UIRect cannot be tweened by tweening the alpha of its proper parent panel.

It looks like the problem is that the UIRect looks for its parent in its OnEnable method, which is called immediately when the prefab is instantiated, before its transform.parent is set in NGUITools.AddChild.  The UIRect.mParentFound is then set to true, preventing the UIRect from finding its proper panel after its transform.parent is set in NGUITools.AddChild.  Adding the following lines to the end of NGUITools.AddChild fixes the problem (but is very dirty :P):

  1. //go is the instantiated prefab
  2. foreach(UIRect rect in go.GetComponentsInChildren<UIRect>())
  3. {
  4.     rect.enabled = false;
  5.     rect.enabled = true;
  6. }
  7.  

This works because UIRect.mParentFound is reset in UIRect.OnEnable() and UIRect.OnDisable().

I look forward to a fix in the near future.  ;D

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIRect.parent not initialized properly when using NGUITools.AddChild
« Reply #1 on: December 18, 2013, 11:51:13 AM »
Thanks, I've added code to ensure that NGUI will delay this operation until after Start() has been executed. I hate the fact that OnEnable() runs right after Awake() and before users have a chance to set properties. It's a stupid blunder on Unity's part.