Changing Music On Icon To Off and saving the preference
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.
Answer by NCPLNaveen · Jan 13, 2016 at 09:35 AM
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
This does not work. It does not change the image and it doesn't correct the issue the OP was having of saving the previous state of the image between scenes.
it works for the music but it doesn't change the icon. but when I open the scene again and the icon changed. For ex: i mute the music, the icon is still the on icon and then I went to another scene. And I come back to that scene but the icon is the off icon. if I click it, still the same icon