- Home /
Enabling/Disabling a slider from script
Hi all,
I have two sliders as a child of a panel(initially disabled within the inspector). What I want is that when I click on a button, I want this panel visible with sliders and when I click the button again I want the panel disappear and goes like this.
GameObject panel;
void Start(){
panel = GameObject.FindGameObjectWithTag("sunPan");
if (panel.activeSelf)
{
panel.SetActive(false);
}
else
{
panel.SetActive(true);
}
}
I added this script to my button. When I run the project I getting following error:
NullReferenceException: Object reference not set to an instance of an object
Is there a way of handling this error?
Comment
add a collider to that button and add one method On$$anonymous$$ouseDown in it...
for example
GameObject panel;
void Start()
{
panel = GameObject.FindGameObjectWithTag("sunPan");
}
void On$$anonymous$$ouseDown()
{
if (panel.activeSelf)
{
panel.SetActive(false);
}
else
{
panel.SetActive(true);
}
}
Hope it helps You... :)
This is temp solution for more you have to refer Unity UI Buttons if you are using unity UI