-2

I have several sliders in my scene that I am trying to reset with a button press. I am unsure how to reference the value field of the Slider component.

Final edit that allows for reset with a GUI button (Thanks everyone for the help and direction):

public class buttonreset : MonoBehaviour
{
    public Button button1;


    Slider[] sliders;

    void Start()
    {
        button1.onClick.AddListener(() => buttonCallBack(button1));
        //Find Sliders
        sliders = FindObjectsOfType<Slider>() as Slider[];
    }

    private void buttonCallBack(Button buttonPressed)
    {

        {
            if (buttonPressed == button1)
            {
                for (int i = 0; i < sliders.Length; i++)
                    sliders[i].value = 0;
                Debug.Log("Clicked: " + button1.name);
            }
        }
    }

    void OnDisable()
    {
        //Un-Register Button Events
        button1.onClick.RemoveAllListeners();
    }
}

Edited again with new code from @PassetCronUs:

public class buttonreset : MonoBehaviour
{
    Slider[] sliders;

    void Start()
    {
        //Find Sliders
        sliders = FindObjectsOfType<Slider>() as Slider[];
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //Loop thrpugh sliders and reset them
            for (int i = 0; i < sliders.Length; i++)
                sliders[i].value = 0;
        }
    }
}

Edited with new script that is sort of working (it resets the first slider in the hierarchy):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class buttonsave : MonoBehaviour
{


    private Slider[] slider;

    void Awake()
    {
        slider = GameObject.FindGameObjectWithTag("slider").GetComponents<Slider>();

    }
    void OnGUI()
    {
        for (int i = 0; i < slider.Length; ++i)
        {
            if (Input.GetMouseButtonDown(0))

            {
                slider[i].value = 0;
            }
        }
    }
}

Old Script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class buttonreset : MonoBehaviour {

        void Start()
        {
           slidertoreset =  GameObject.FindGameObjectWithTag("slider").GetComponents<Slider>();
        }
            void OnGUI()
        {

           if (Input.GetMouseButtonUp(0))
            {
                Slider.value = 0;
            }
        }
   }

Looking for help with this, thanks.

cyo
  • 69
  • 1
  • 14
  • Add Edit and your new script below it not above the original code. Also, didn't you read the answer? It says don't use `OnGUI` but you still did. Don't just jump to code in answers. Read the text too. – Programmer Mar 27 '18 at 19:17
  • Yes, but what if I want to use a button... – cyo Mar 27 '18 at 19:24
  • *"Edited again with new code from @Programmer:"* That answer is not mine. It came from another user named PassetCronUs. I think he/she answered your above question under the comment section in the answer but I will link that.See **#2** from [this](https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects/41392130#41392130) post for how to detect clicks on UI buttons. – Programmer Mar 27 '18 at 19:31

1 Answers1

1

Find all the Sliders component with FindObjectsOfType, store them in an array then loop over them and rest them when mouse button is clicked.

Slider[] sliders;

void Start()
{
    //Find Sliders
    sliders = FindObjectsOfType<Slider>() as Slider[];
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        //Loop thrpugh sliders and reset them
        for (int i = 0; i < sliders.Length; i++)
            sliders[i].value = 0;
    }
}

Do not use the the OnGUI function. That's used for the old UI system. The Update function is fine for this.

PassetCronUs
  • 447
  • 4
  • 11
  • Thank you, this work great. My only problem now is that I am trying to control this with a GUI button... – cyo Mar 27 '18 at 19:16
  • That wasn't your original question but you can use `Button.onClick.AddListener` to detect click on a UI button then do what's in that `for` loop in my answer. Create new question if you don't know how to do this but make sure to do some googling first before creating new question for this – PassetCronUs Mar 27 '18 at 19:24