Author Topic: NullReferenceException in UIRect.GetLocalPos  (Read 3352 times)

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
NullReferenceException in UIRect.GetLocalPos
« on: December 10, 2013, 04:25:22 PM »
I may be doing something wrong or using the new anchor system in an unintended way, but…

I have a prefab containing a UIPanel with soft clipping anchored to a parent transform, basically set up to fill the screen minus some padding.  In the Start method of a component of mine in the prefab, I'm modifying the anchor's absolute values based on some other state, then calling UpdateAnchors on the panel so that GetViewSize will return the correct value:

  1. scrollView.panel.bottomAnchor.absolute += footerPadding;
  2. scrollView.panel.UpdateAnchors();

This causes the following exception:

  1. NullReferenceException: Object reference not set to an instance of an object
  2. UIRect.GetLocalPos (.AnchorPoint ac, UnityEngine.Transform trans) (at Assets/Plugins/Client/NGUI/Scripts/Internal/UIRect.cs:212)
  3. UIPanel.OnAnchor () (at Assets/Plugins/Client/NGUI/Scripts/UI/UIPanel.cs:758)
  4. UIRect.UpdateAnchors () (at Assets/Plugins/Client/NGUI/Scripts/Internal/UIRect.cs:309)
  5. MyClass.Start () (at Assets/Scripts/.../MyClass.cs:344)
  6.  

I was able to fix the error by modifying UpdateAnchors in UIRect:

  1.         public void UpdateAnchors ()
  2.         {
  3.                 if (!mAnchorsCached) CacheAnchors();
  4.                 if (isAnchored) OnAnchor();
  5.         }
  6.  

I'm not sure if this is the best fix, or if I should be calling something else from my code to get things to recalculate.  I need to be able to get the correct size of the scroll view later in my Start method to be able to correctly size and position some other elements.

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: NullReferenceException in UIRect.GetLocalPos
« Reply #1 on: December 10, 2013, 07:11:31 PM »
Related problem:  GetViewSize is giving me bad results on retina iPads.  My UIRoot is set to fixed size with a manual height of 768.  However, it looks like the panel isn't finding the UIRoot in UIRect.OnEnable.  I added a check in UIPanel's OnStart to attempt to discover the root again if it's still null, but that's just a quick workaround.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NullReferenceException in UIRect.GetLocalPos
« Reply #2 on: December 10, 2013, 09:22:02 PM »
You don't need to call UpdateAnchors as it will should happen on the next frame regardless. I noticed there was a small issue with how the camera reference gets set in 3.0.7 f1, which I've already resolved in the Pro repository. I just can't update right now because of the sale.

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: NullReferenceException in UIRect.GetLocalPos
« Reply #3 on: December 11, 2013, 02:02:29 PM »
I can't wait until the next frame.  I'm instantiating a bunch of prefabs into the panel right after I adjust the anchors, and the size and position of those prefabs changes based on the clip rect of the panel.  (Think buttons that change in height down to a minimum size, then go from two rows of buttons to one if two rows don't fit.)  OK, technically I could wait until the next frame to instantiate my prefabs, but I'd rather not.  But it sounds like UpdateAnchors would be the right thing to call in that case, and that you've fixed the bug in some other way, so thanks!