Author Topic: NGUI Buttons interacting with a NGUI slider value  (Read 10360 times)

HypoXic5665

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
NGUI Buttons interacting with a NGUI slider value
« on: October 14, 2012, 04:08:02 PM »
Hello, I currently have a slider that can be adjusted via the slider itself or one of two NGUI buttons (up and down). When the "up" or "down" button is pressed the value increases or decreases as expected. However, pressing the "up" folowed by the "down" button causes the value to increase by one, then continued presses on the "down" button act as expected.

Example:
Power = 300
Press up button: Power = 301
Press up button: Power = 302
Press down button: Power = 303 << This is the problem
Press down button: Power = 302
Press down button: Power = 301

Here is the code that is on the NGUI Slider:
  1. static var power: int; //power that will be passed to the egg when shot
  2. var slider: UISlider; //will become the <Power_Slider>s <UISlider> component
  3.  
  4. //values for the slider
  5. var minPower: int = 200; //the min power that the slider will be able to input
  6. var maxPower: int = 500; //the max power that the slider will be able to input
  7.  
  8. function Start()
  9. {
  10.         slider = gameObject.GetComponent(UISlider); //get the <UISlider> component of the power slider
  11. }
  12.  
  13. function Update ()
  14. {
  15.         power = Mathf.Lerp(minPower,maxPower,slider.sliderValue); //set the sliders min and max values
  16. }

And here is the code on the NGUI Buttons:
  1. var powerSlider: UISlider; //to get and adjust the slider value
  2.  
  3. private var sliderInc: float = 0.003335;
  4.  
  5. function OnMouseUpAsButton()
  6. {
  7.         if(gameObject.name == "Power_Button_Up") //if the <Up_Button> is pressed then increase the slider value...
  8.         {
  9.                 powerSlider.sliderValue = powerSlider.sliderValue + sliderInc;
  10.                 print(gameObject.name + "s power is: " + GameObject.Find("Power_Slider").GetComponent(PowerSlider).power);
  11.         }else //else the down button was pressed so decrease the slider value...
  12.         {
  13.                 powerSlider.sliderValue = powerSlider.sliderValue - sliderInc;
  14.                 print(gameObject.name + "s power is: "  + GameObject.Find("Power_Slider").GetComponent(PowerSlider).power);
  15.         }      
  16. }

Any help will be appreciated to lead me in the right direction to fix this issue.

Thanks in advance,

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI Buttons interacting with a NGUI slider value
« Reply #1 on: October 14, 2012, 05:33:08 PM »
What is "OnMouseUpAsButton"? NGUI sends OnPress(true/false), and OnClick().

HypoXic5665

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: NGUI Buttons interacting with a NGUI slider value
« Reply #2 on: October 14, 2012, 05:53:47 PM »
Description from Unity Scripting Reference-
"OnMouseUpAsButton() is only called when the mouse is released over the same GUIElement or Collider as it was pressed."

It appears to act the same as OnClick(). The problem still occurs with OnClick() or OnPress() as well. Is this method even the correct or most direct way to go about changing the slider value via a separate button in NGUI?

Thanks,

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: NGUI Buttons interacting with a NGUI slider value
« Reply #3 on: October 15, 2012, 03:30:59 AM »
You are setting member variable "sliderValue" and then immediately reading the value of member variable "power".  Why are you expecting a change of the one variable to have an immediate change of the other variable of a different gameObject if you aren't explicitly calling code to synchronize those two?