0
\$\begingroup\$

I have a script in which I defined different functions for each button and assign them their appropriate functions. I defined if statements for them and now I want every button to play it's music when if is true. in OnClick I assigned the script but it didn't work and when I added AudioSurce to it under play () function, it played my sound on button click not when if was true. here is my script;

public class MainMenu3 : MonoBehaviour {


    [SerializeField] Button WhitePlane;
    [SerializeField] GameObject wood1;


    public static int chnum;
    public bool isPlanesold;


void Start () {

        PlayerPrefs.SetString ("BluePlane", "on");
        if (PlayerPrefs.GetString ("WhitePlane") == "on") {
            wood1.SetActive (false);
        }

        UIManager2.coin_score = PlayerPrefs.GetInt ("Score");
        isPlanesold = PlayerPrefs.GetInt ("isPlanesold") == 0 ? false:true;
}

public void BuyWhitePlane () {

        if (PlayerPrefs.GetString ("WhitePlane") == "on") { 
            PlayerPrefs.SetInt ("isPlanesold", 1);
            chnum = 2;
            PlayerPrefs.Save ();
            SceneManager.LoadScene ("Menu2");
        } else if (UIManager2.coin_score >= 1) {
            this.GetComponent<AudioSource> ().Play ();
            PlayerPrefs.SetInt ("isPlanesold", 1);
            chnum = 2;
            PlayerPrefs.SetInt ("Score", UIManager2.coin_score--);
            PlayerPrefs.SetInt ("Score", UIManager2.coin_score);
            PlayerPrefs.Save ();
            PlayerPrefs.SetString ("WhitePlane", "on");
            SceneManager.LoadScene ("Menu2");

            }
        else 
        {
            Debug.Log ("You Don't Have The Score");

        }
}
\$\endgroup\$
4
  • \$\begingroup\$ You can't assign a script to a button handler. Only a method of a specific instance of a script. Are you sure that the instance of the script you assigned to each button onclick event list is on a different game object with a correctly set up audio source? \$\endgroup\$
    – Philipp
    Apr 23, 2020 at 15:23
  • \$\begingroup\$ I attached this script to my Canvas and then attached my audio to it. for OnClick I assigned Canvas to each button and then selected my function for each button through the script I've already assigned to Canvas. \$\endgroup\$
    – amin007
    Apr 23, 2020 at 15:30
  • \$\begingroup\$ ah, so you are only showing us one of many click handlers? Well, if they are all on the same game object, then they of course all trigger the same AudioSource which always have the same audio clip assigned to it. \$\endgroup\$
    – Philipp
    Apr 23, 2020 at 15:33
  • \$\begingroup\$ I see only one call to AudioSource.Play() here, and it's in your else if case - is that the one you mean? You're loading a new scene as soon as you start playing the sound, which immediately stops playing the sound because you've unloaded the object that was trying to play it. \$\endgroup\$
    – DMGregory
    Apr 23, 2020 at 15:39

0

You must log in to answer this question.

Browse other questions tagged .