- Home /
How to make UI checkbox appear or disappear in start function?
I have added a UI Toggle to my main menu in order to mute the game audio on click. Everything works fine but the only problem is that no matter whether the Audio Listener volume is on 0 or 1, the checkbox is always shown when the main menu opens. How do I only make the checkbox appear when Audio Listener Volume is 1?
Thanks in advance :)
Answer by UsulPro · Sep 28, 2015 at 10:29 AM
Hi, @RSharma98! try this code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class checkb : MonoBehaviour {
public Toggle toggle;
public AudioSource audioSrs;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(audioSrs.volume>=1) {
toggle.isOn=true;
} else {
toggle.isOn=false;
}
}
}
Hi, thank you for your answer. Your script contains similar references to what I need and I have altered it slightly to fit my needs. However, when I write the variable ;
public Toggle toggle;
I get the error saying "The type or namespace name `Toggle' could not be found. Are you missing a using directive or an assembly reference?"
Do you know how I can fix this as I have tried searching yet can not find a fix. (If not it is ok) And once again thank you very much for answering @UsulPro
@RSharma98 make sure to add this line
using UnityEngine.UI;
if you are using any UI related Classes.
Your answer
Follow this Question
Related Questions
[4.6] Toggle/Checkbox On/Off 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Cant access .interactable 1 Answer
Toggle UI 4.6 0 Answers