Adding a listener to Toggle.onValueChanged via Script

Hello,

I was trying to add a listener on a Toggle Button but i am not able to write that. Can anybody please help me. Here is what i am doing

Toggle sound = GameObject.Find("soundOnOff").GetComponent();
sound.onValueChanged.AddListener(gameSoundStatusChanged); // --- Error

and the function is

public void gameSoundStatusChanged()
{....}

it says .addlistener has some invalid parameters. Cause it asks for parameter of type UnityAction function.
Please help me on how to write the function in given syntax.

I just had the same issue. You just need to pass a float parameter into your method.
eg.

	public Slider myMusicVolumeSlider;
	AudioSource myBGMusic;

	void Start () 
	{
		myBGMusic = GetComponent<AudioSource>();
		myMusicVolumeSlider.value = 0.2f;
		myMusicVolumeSlider.onValueChanged.AddListener(RaiseVolume);
	}

	void Update () 
	{
		//I Don't want to run this every frame....
		//myBGMusic.volume = myMusicVolumeSlider.value;
	}

	public void RaiseVolume(float value)
	{
		myBGMusic.volume = myMusicVolumeSlider.value;
		//Or even just... myBGMusic.volume = value;
	}

Only just like this:

Toggle sound = GameObject.Find(“soundOnOff”).GetComponent();
sound.onValueChanged.AddListener(gameSoundStatusChanged);

public void gameSoundStatusChanged(bool isclick)
{…}

your method gameStatusChanged() needs to take bool param
so change it to: gameStatusChanged(bool newValue) and it will work fine