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.