Hey guys,
I have different background music clips that should play even if I restart the game.
For that purpose I have created a Script attached on a gameObject that only plays the music.
private var player : GameObject;
var mg : AudioClip;
var def : AudioClip;
function Awake(){
DontDestroyOnLoad(this.gameObject);
if (FindObjectsOfType(GetType()).Length > 1){
Destroy(gameObject); //Avoid duplicate on scene restart
}
}
function Start () {
player = GameObject.Find("Player");
}
function Update () {
if(PlayerPrefs.GetInt("TimesPlayed") >= PlayerPrefs.GetInt("OverallScore")){
GetComponent.().clip = mg;
GetComponent.().Play();
}
if(player.transform.GetChild(0).gameObject.name == "CubeDefault"){
GetComponent.().clip = def;
GetComponent.().Play();
}
}
The problem is that the specified clip is called every frame since it is in the update function, but it needs to be in this function, because
- at Runtime the music can change,
- the script will never be destroyed and so I can not put this code to the start/awake function as they
read the content only once.
The solution would be some kind of "if" that change their values to call
`GetComponent.().Play();`
only once, but how can I do this?
Thanks
↧