Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Slider OnValue Changed Not Updating in Runtime.

Discussion in 'Unity 5 Pre-order Beta' started by TroyDavis, Jan 15, 2015.

  1. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    I have attached a script to my slider control called FillUpdate, this code simply updates a textbox with the current value of the Slider. I have clicked the plus sign on On Value Changed (single) selected Editor and Runtime then selected the Script on the object it is attached to which is the Sliderbar. and lastly I selected the function which is public from the script.

    Code (CSharp):
    1. /// <summary>
    2. /// FillUpdate.cs
    3. /// Adam T. Davis
    4. /// Delinquent Game Studios
    5. /// Copyright © 2013 - 2015 All Rights Reserved.
    6. /// 01/14/2015
    7. ///
    8. /// Updates the Slider Textbox.
    9. /// </summary>
    10. using UnityEngine;
    11. using UnityEngine.UI;
    12. using System.Collections;
    13.  
    14. public class FillUpdate : MonoBehaviour {
    15.  
    16.     public Text FillText;
    17.      
    18.     // Update the Silder Textbox
    19.     public void UpdateFillTexture () {
    20.         Debug.Log(gameObject.GetComponent<Slider>().value.ToString());
    21.         FillText.text = gameObject.GetComponent<Slider>().value.ToString();
    22.     }
    23. }
    This works perfectly in Edit mode but when running the text box does not update, nor does my Debug.Log(); It's as if the Runtime simply isn't connected to the Slider.
     
    Last edited: Jan 15, 2015
  2. Caio_Lib

    Caio_Lib

    Joined:
    Mar 4, 2014
    Posts:
    83
    Hi TroyDavis,
    You can change your code to:

    Code (CSharp):
    1. /// <summary>
    2. /// FillUpdate.cs
    3. /// Adam T. Davis
    4. /// Delinquent Game Studios
    5. /// Copyright © 2013 - 2015 All Rights Reserved.
    6. /// 01/14/2015
    7. ///
    8. /// Updates the Slider Textbox.
    9. /// </summary>
    10. using UnityEngine;
    11. using UnityEngine.UI;
    12. using System.Collections;
    13.  
    14. public class FillUpdate : MonoBehaviour
    15. {
    16.  
    17.     public Text FillText;
    18.  
    19.     // Update the Silder Textbox
    20.     public void UpdateFillTexture(float value)
    21.     {
    22.         Debug.Log(value.ToString());
    23.         FillText.text = value.ToString();
    24.     }
    25. }
    Then change OnValueChanged dropdown to FillUpdate -> UpdateFillTexture below Dynamic Float.
    This should work.
     
  3. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    No it's still not changing during runtime in 5.0 b19 my method and your method both work as expected in the editor if you change the slider value the textbox updates as expected, but when you run the game the slider graphic can be adjusted by the value of the slider but the FillUpdate Script isn't triggered. below is a video demonstrating the issue. You'll notice in editor mode the textbox updates just fine, but when the game is run the slider works but the text does not update.

     
  4. Caio_Lib

    Caio_Lib

    Joined:
    Mar 4, 2014
    Posts:
    83
    You're right, same happened here!
    When I changed slider value in the player it worked, but changing slider value in editor it doesn't as you showed.
     
  5. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    If you make the slider interactive it works fine in run-time when you drag the bar but if you try to adjust the value by code at run-time it doesn't. It's definitely a bug.
     
  6. Caio_Lib

    Caio_Lib

    Joined:
    Mar 4, 2014
    Posts:
    83
    Sure you found a bug!
    When you are in runtime and change values in Editor OnValueChanged ( Editor And Runtime ) doesn't trigger.
    But I could change Slider value by code in runtime even if it's not interactive and OnValueChanged is triggering. How are you changing Slider value?

    Here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Example : MonoBehaviour {
    6.  
    7.     public Slider slider;
    8.  
    9.     void Update()
    10.     {
    11.         slider.value += (Input.GetKey(KeyCode.LeftArrow) ? -0.1f : 0);
    12.         slider.value += (Input.GetKey(KeyCode.RightArrow) ? 0.1f : 0);
    13.     }
    14. }
    15.  
    I added this code to the camera and added Slider to it. But I would report as a bug anyway!
     
  7. TroyDavis

    TroyDavis

    Joined:
    Mar 27, 2013
    Posts:
    78
    I set the silder value via a messenger.

    Messenger.Broadcast ("player health update", curHealth, maxHealth);

    From that I just send the value the slider should be at.

    Code (CSharp):
    1. void Start(){
    2.         LoadCharacter();
    3.         agility = GetPrimaryAttribute ((int)AttributeName.Agility).BaseValue;
    4.         combat = GetPrimaryAttribute ((int)AttributeName.Combat).BaseValue;
    5.         //body = GetPrimaryAttribute ((int)AttributeName.Body).BaseValue;
    6.         body = 6;
    7.         charisma = GetPrimaryAttribute ((int)AttributeName.Charisma).BaseValue;
    8.         intellect = GetPrimaryAttribute ((int)AttributeName.Intellect).BaseValue;
    9.         //willPower = GetPrimaryAttribute ((int)AttributeName.WillPower).BaseValue;
    10.         willPower = 3;
    11.         maxHealth = body;
    12.         //curHealth = maxHealth;
    13.         curHealth = 3;
    14.         maxQFE = willPower;
    15.         //curQFE = maxQFE;
    16.         curQFE = 2;
    17.  
    18.         Messenger.Broadcast ("player health update", curHealth, maxHealth);
    19.         Messenger.Broadcast ("player QFE update", curQFE, maxQFE);
    20.  
    21.     }
    I modified my FillUpdate to handle the Messenger Listener as follows:

    Code (CSharp):
    1. /// <summary>
    2. /// FillUpdate.cs
    3. /// Adam T. Davis
    4. /// Delinquent Game Studios
    5. /// Copyright © 2013 - 2015 All Rights Reserved.
    6. /// 01/14/2015
    7. ///
    8. /// Updates the Slider Textbox.
    9. /// </summary>
    10. using UnityEngine;
    11. using UnityEngine.UI;
    12. using System.Collections;
    13.  
    14. public class FillUpdate : MonoBehaviour {
    15.  
    16.     public Text FillText;
    17.     public bool Player;
    18.     public bool Healthbar;
    19.        
    20.     // Update the Silder Textbox
    21.     public void UpdateFillTexture (float value) {
    22.         Debug.Log("Made it here!");
    23.         FillText.text = Mathf.Round(value * 100f).ToString() + "% / 100%";
    24.         if (Mathf.Round(value * 100f) == 0)
    25.             HideSlider();
    26.         else
    27.             ShowSlider();
    28.     }
    29.  
    30.     void ShowSlider(){
    31.         gameObject.SetActive(true);
    32.     }
    33.  
    34.     void HideSlider(){
    35.         gameObject.SetActive(false);
    36.     }
    37.  
    38.     public void OnEnable () {
    39.         Messenger.AddListener<float, float>("player health update", OnChangePlayerHealth);
    40.         Messenger.AddListener<float, float>("player QFE update", OnChangePlayerQuantumFieldEnergy);
    41. //        Messenger.AddListener<string>("set player name", SetPlayerName);
    42. //        Messenger.AddListener<float, float>("enemy health update", OnChangeEnemyHealthBarSize);
    43. //        Messenger.AddListener<float, float>("enemy QFE update", OnChangeEnemyQuantumFieldEnergyBarSize);
    44. //        Messenger.AddListener<string>("set enemy name", SetEnemyName);  
    45.     }
    46.  
    47.     private void OnChangePlayerHealth (float _curValue, float _maxValue) {
    48.         if (Player && Healthbar)
    49.             gameObject.GetComponent<Slider>().value = _curValue / _maxValue;
    50.     }
    51.  
    52.     private void OnChangePlayerQuantumFieldEnergy (float _curValue, float _maxValue) {
    53.         if (Player && !Healthbar)
    54.             gameObject.GetComponent<Slider>().value = _curValue / _maxValue;
    55.     }
    56. }
    57.  
    Updating the value this way seems to work fine. So the bug is only with manually sliding the value bar on the Inspector while the game is running.
     
    Last edited: Jan 15, 2015
    Caio_Lib likes this.