Author Topic: UISlider - changing its position and appearance by script?  (Read 6744 times)

AxelF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Hi,

I'm a happy new NGUI customer and want to say a big "thank you" at first - it saves me a lot of time. However, now that I'm digging deeper into all its features I encountered my first problem: I'm setting the value of an UISlider object in my script and hoped it would update its appearance automatically afterwards (button position e.g.). Well - it's not working and I wonder why. Is it not enough to simply change the variable "value"? What else is needed so the slider updates itself correctly?

Thanks in advance for any help!

Regards, Axel

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider - changing its position and appearance by script?
« Reply #1 on: May 20, 2014, 02:12:25 PM »
Changing the UISlider's 'value' is enough. What's the full code you're using here?

AxelF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UISlider - changing its position and appearance by script?
« Reply #2 on: May 20, 2014, 03:25:04 PM »
Its a simple call of my my slider object like "slider.value = 1" in the Start() function of another GUI object. I guess the problem is somewhere in my code then. But it's good to know it should work at least.

rahares

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: UISlider - changing its position and appearance by script?
« Reply #3 on: May 20, 2014, 09:00:01 PM »
This might help, its how I update a UISlider as a healthbar

using UnityEngine;
using System.Collections;

public class VitalBar : MonoBehaviour {

   public UILabel healthBarLabel;

   private UISlider vitalSlider;
   private float _maxWidth;
   private bool _displayText = false;

   void Awake (){
      vitalSlider = GetComponent<UISlider> ();

      if (vitalSlider == null) {
         Debug.LogError("Could not find the UISlider Component!");
         return;
            }
      _maxWidth = vitalSlider.foregroundWidget.localSize.x;
      _displayText =true;
   }


   // Use this for initialization
   void Start () {
      UpdateDisplay (- 5f, "100/200");
   
   }
   //vitalSlider.sliderValue = health/healthBarLabel;
   public void UpdateDisplay(float x ){
      if (x > 0)
         x = 0;
      else if (x > 1)
         x = 1;
      vitalSlider.value = 50;
      DisplayText = false;
   }

   public void UpdateDisplay(float x, string str ){
      UpdateDisplay (x);
      if (str != "") healthBarLabel.text = str;
   }

   public bool DisplayText {
      get { return _displayText;}
      set { _displayText = value;
          if (!_displayText)
            healthBarLabel.text = ""; }
   }
}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider - changing its position and appearance by script?
« Reply #4 on: May 20, 2014, 11:07:48 PM »
Slider's value goes from 0 to 1. You're passing '50'.

AxelF

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: UISlider - changing its position and appearance by script?
« Reply #5 on: May 21, 2014, 06:05:25 AM »
Wow, that is strange. If the parent GameObject of the slider (a UISprite) contains a Tween Scale component I can not set the value visually of the slider object. If I deactivate the Tween component it works. I have absolutely no explanation for this behavior. If the parent object is a panel the problem persists, so it's not UISprites fault.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider - changing its position and appearance by script?
« Reply #6 on: May 21, 2014, 12:08:17 PM »
TweenScale from which value? If it's zero, then that's an invalid scale (you can't divide by zero).

AjitR

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: UISlider - changing its position and appearance by script?
« Reply #7 on: July 03, 2014, 01:24:52 PM »
if we use 'public UISlider slideBAR'....then what should be given as reference for this...
I tried this but i couldn't find any 'UISlider' for referencing ..