I want to create a button playing of music with a button but I don't understand how can I get the button and do functions with it. In addition to making music stop and start, I also want to change the button image to indicate whether the music is enabled. This is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SoundControl : MonoBehaviour {
// Use this for initialization
private Button note;
void Start () {
PlayerPrefs.SetInt ("Music", 1);
note = GetComponent<Button> ();
}
// Update is called once per frame
public Sprite onnote;
public Sprite offnote;
void Update () {
if (PlayerPrefs.GetInt ("Music") == 1) {
note.image.overrideSprite = onnote;
Debug.Log("Button Music equals 1 UPdated");
}
if (PlayerPrefs.GetInt ("Music") == 0) {
note.image.overrideSprite = offnote;
Debug.Log("Button Music equals 0 UPdated");
}
}
public void MakeSound()
{
Debug.Log("Button Music pressed");
if (PlayerPrefs.GetInt ("Music") == 1) {
PlayerPrefs.SetInt ("Music", 0);
Debug.Log("Button Music equals 0");
}
if (PlayerPrefs.GetInt ("Music") == 0) {
PlayerPrefs.SetInt ("Music", 1);
Debug.Log("Button Music equals 1");
}
}
}
So i put this script on Button.
Notice i have changed my code but now the console is showing this: