Hi,
Finishing up a game i have been working on for a while, i came the problem of changing the icon of the mute music. I have the icon images and everything, however i am not able to find a way that changes the music icon to a music-off icon when pressed. So basically, what i have now is a UI button that when pressed, mutes the music in the game. However i can’t figure out how to change the icon when the music has been muted and to save that preference so that when the player changes scenes and comes back to the scene where the icon is, it should show a music-off icon if it had been muted.
I hope i was a bit clear with my description :).
Thanks.
Hey @Meshari94, Even i was having the same problem, I come up with a solution, try this create a Empty game object in the scene and add this script which im going to write down.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Soundonandoff : MonoBehaviour {
public Button b;
public bool musicMute;
public Sprite musicOn;
public Sprite musicOff;
void Start () {
musicMute = false;
if (AudioListener.volume == 1) {
b.image.sprite = musicOn;
} else {
b.image.sprite = musicOff;
}
}
public void Musiconandoff(){
musicMute = !musicMute;
if (musicMute) {
AudioListener.volume = 0;
}
} else if (!musicMute) {
AudioListener.volume = 1;
}
}
}
}
and then create a button in UI and place it at correct location. Then give the above script to created empty game object and assign the above created button as well as the images that you want to display to the script. On the button you will find OnClick() function add the empty gameobject and also the function Musiconandoff() function. thats it