-1

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: enter image description here

UnityNewb
  • 361
  • 1
  • 5
  • 17
  • make a button and when clicked, have it run `MakeSound()` ...... – maraaaaaaaa Oct 29 '15 at 20:04
  • That its not what i ment, i want when it be clicked the button will change picture to gray note and the music will stop, and when i click again the note will come back to original and sound will play, i wrote what i need in the gray lines on update. – UnityNewb Oct 29 '15 at 22:12
  • Solved!!!!, By using switch i solved that when i use if(), i can break out and it wont repeat itself – UnityNewb Oct 31 '15 at 17:57

1 Answers1

0

this is how i correct 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()
{
    switch(PlayerPrefs.GetInt("Music"))
    {
    case 0:
        PlayerPrefs.SetInt ("Music", 1);
        Debug.Log("Button Music equals 1");
        break;
    case 1:
        PlayerPrefs.SetInt ("Music", 0);
        Debug.Log("Button Music equals 0");
        break;
    }
}
}

when i use if i can't break out from the loop :) so i used switch to break when i need.

UnityNewb
  • 361
  • 1
  • 5
  • 17