Author Topic: ScrollView + UISlider problem  (Read 9467 times)

r.pedra

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 20
  • Posts: 131
    • View Profile
ScrollView + UISlider problem
« on: March 12, 2014, 01:52:20 PM »
Hi,
I updated NGUI on monday to the last version 3.5.3 but I have a problem I hadn't before.
I have an unrestricted UIScrollview that is linked to an UISlider.

Every time I scroll the panel by touch or click I got this error:
Quote
NullReferenceException: Object reference not set to an instance of an object
UIScrollView.UpdateScrollbars (.UIScrollBar sb, Single contentMin, Single contentMax, Single contentSize, Single viewSize, Boolean inverted) (at Assets/NGUI/Scripts/Interaction/UIScrollView.cs:494)
UIScrollView.UpdateScrollbars (Boolean recalculateBounds) (at Assets/NGUI/Scripts/Interaction/UIScrollView.cs:441)
UIScrollView.MoveRelative (Vector3 relative) (at Assets/NGUI/Scripts/Interaction/UIScrollView.cs:632)
UIScrollView.MoveAbsolute (Vector3 absolute) (at Assets/NGUI/Scripts/Interaction/UIScrollView.cs:643)
UIScrollView.LateUpdate () (at Assets/NGUI/Scripts/Interaction/UIScrollView.cs:874)
If I don't touch the ScrollView but I slide the UISlider, it does nothing.

I attached the configuration of the UIScrollView and UISlider to the topic.

Thank you.

EDIT: I did more tests and I debugged the variable 'sb' of the line throwing NullRefException
  1. Debug.Log ("HorizontalScrollBar:"+sb);
returns:
Quote
HorizontalScrollBar:
So the UIScrollbar seems to be NULL.
But when I debug it before the function call, it's not null...

EDIT2:
Little more test
  1. Debug.Log ("ScrollBar:"+ (horizontalScrollBar as UIScrollBar));
returns null. You're casting the UIProgressBar to UIScrollBar but it's breaking it... Can I remove the cast without breaking all NGUI?
« Last Edit: March 12, 2014, 02:08:24 PM by r.pedra »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: ScrollView + UISlider problem
« Reply #1 on: March 12, 2014, 09:21:31 PM »
Hmm. Yes you can. I'll do a similar change on my end for the next version. Thanks for bringing it to my attention.
  1.         protected void UpdateScrollbars (UIProgressBar slider, float contentMin, float contentMax, float contentSize, float viewSize, bool inverted)
  2.         {
  3.                 if (slider == null) return;
  4.  
  5.                 if (viewSize < contentSize)
  6.                 {
  7.                         contentMin = Mathf.Clamp01(contentMin / contentSize);
  8.                         contentMax = Mathf.Clamp01(contentMax / contentSize);
  9.                 }
  10.                 else
  11.                 {
  12.                         contentMin = Mathf.Clamp01(-contentMin / contentSize);
  13.                         contentMax = Mathf.Clamp01(-contentMax / contentSize);
  14.                 }
  15.  
  16.                 mIgnoreCallbacks = true;
  17.                 {
  18.                         float contentPadding = contentMin + contentMax;
  19.                         slider.value = inverted ? ((contentPadding > 0.001f) ? 1f - contentMin / contentPadding : 0f) :
  20.                                 ((contentPadding > 0.001f) ? contentMin / contentPadding : 1f);
  21.  
  22.                         UIScrollBar sb = slider as UIScrollBar;
  23.                         if (sb != null) sb.barSize = 1f - contentPadding;
  24.                 }
  25.                 mIgnoreCallbacks = false;
  26.         }
...and when calling UpdateScrollbars, remove the "as UIScrollBar" cast:
  1. UpdateScrollbars(verticalScrollBar, contentMin, contentMax, contentSize, viewSize, true);