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");
}
}
AudioSource.Play()
here, and it's in yourelse 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\$